Advertisement
Thibstars

Enforce numeric input

Jul 26th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ** Returns the caret (cursor) position of the specified text field.
  3. ** Return value range is 0-oField.value.length.
  4. */
  5. function getCaretPosition (oField) {
  6.  
  7.   // Initialize
  8.   var iCaretPos = 0;
  9.  
  10.   // IE Support
  11.   if (document.selection) {
  12.  
  13.     // Set focus on the element
  14.     oField.focus();
  15.  
  16.     // To get cursor position, get empty selection range
  17.     var oSel = document.selection.createRange();
  18.  
  19.     // Move selection start to 0 position
  20.     oSel.moveStart('character', -oField.value.length);
  21.  
  22.     // The caret position is selection length
  23.     iCaretPos = oSel.text.length;
  24.   }
  25.  
  26.   // Firefox support
  27.   else if (oField.selectionStart || oField.selectionStart == '0')
  28.     iCaretPos = oField.selectionStart;
  29.  
  30.   // Return results
  31.   return iCaretPos;
  32. }
  33.  
  34. /* Return the selected text in the document */
  35. function getSelectionText() {
  36.     var text = "";
  37.     if (window.getSelection) {
  38.         text = window.getSelection().toString();
  39.     } else if (document.selection && document.selection.type != "Control") {
  40.         text = document.selection.createRange().text;
  41.     }
  42.     return text;
  43. }
  44.  
  45. /* Enforce numeric input in apex number fields (which are strangely input="text" iso input="number")
  46.     NOTE: in this implementation we assume that decimal separators can be either '.' or ','. */
  47. function makeFieldsNumericOnly(numberFields) {
  48.         numberFields.keydown(function (e) {
  49.         var oldVal = e.target.value;
  50.         var first = oldVal.substr(0, getCaretPosition(e.target));
  51.         var last = oldVal.substr(getCaretPosition(e.target));
  52.         var newVal = first + e.key + last;
  53.         if (e.key === ' ') { // Prevent space straight away
  54.             e.preventDefault();
  55.         } else {
  56.             if (getSelectionText() == oldVal) {
  57.                 // The old value was selected, make the entered key the newVal
  58.                 newVal = e.key;
  59.             }
  60.             if (
  61.                 // Allow following input
  62.                 ((e.key === '+' || e.key === '-') && newVal.substr(1).indexOf(e.key) == -1) ||
  63.                 (newVal.indexOf(e.key) === 0 && e.key === ',') ||
  64.                 (newVal.indexOf(e.key) === 0 && e.key === 'Decimal') || // For '.' in IE...
  65.                 (e.key === 'Backspace') ||
  66.                 (e.key === 'Tab') ||
  67.                 (e.key === 'Insert') ||
  68.                 (e.key ==='Delete') ||
  69.                 (e.key === 'ArrowLeft' || e.key === 'ArrowRight') ||
  70.                 (e.key === 'Home' || e.key === 'End') ||
  71.                 ((e.keyCode === 65 || e.keyCode === 67 || e.keyCode === 86) && (e.ctrlKey === true || e.metaKey === true)) // Ctrl+A/C/V, Command+A/C/V
  72.             ) {
  73.                 return; // Let it happen
  74.             } else if (isNaN(newVal.replace(',', '.'))) {
  75.                 // Not a number, block input
  76.                 e.preventDefault();
  77.             } else if(newVal.indexOf(',') >= 0 && e.key != ',') { // Allow only 1 decimal
  78.                 var decimals = newVal.substr(newVal.indexOf(',') + 1);
  79.                 if (decimals.length > 1) {
  80.                     e.preventDefault();
  81.                 }
  82.             } else if(newVal.indexOf('.') >= 0 && e.key != '.') { // Allow only 1 decimal
  83.                 var decimals = newVal.substr(newVal.indexOf('.') + 1);
  84.                 if (decimals.length > 1) {
  85.                     e.preventDefault();
  86.                 }
  87.             }
  88.         }          
  89.        
  90.     });  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement