Thibstars icon

Enforce numeric input

Thibstars | PRO | 07/26/17 09:33:58 AM UTC | 0 ⭐ | 319 👁️ | Never ⏰ | []
JavaScript |

3.4 KB

|

None

|

0 👍

/

0 👎

/*
** Returns the caret (cursor) position of the specified text field.
** Return value range is 0-oField.value.length.
*/
function getCaretPosition (oField) {
 
  // Initialize
  var iCaretPos = 0;
 
  // IE Support
  if (document.selection) {
 
    // Set focus on the element
    oField.focus();
 
    // To get cursor position, get empty selection range
    var oSel = document.selection.createRange();
 
    // Move selection start to 0 position
    oSel.moveStart('character', -oField.value.length);
 
    // The caret position is selection length
    iCaretPos = oSel.text.length;
  }
 
  // Firefox support
  else if (oField.selectionStart || oField.selectionStart == '0')
    iCaretPos = oField.selectionStart;
 
  // Return results
  return iCaretPos;
}
 
/* Return the selected text in the document */
function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}
 
/* Enforce numeric input in apex number fields (which are strangely input="text" iso input="number")
    NOTE: in this implementation we assume that decimal separators can be either '.' or ','. */
function makeFieldsNumericOnly(numberFields) {
        numberFields.keydown(function (e) {
        var oldVal = e.target.value;
        var first = oldVal.substr(0, getCaretPosition(e.target));
        var last = oldVal.substr(getCaretPosition(e.target));
        var newVal = first + e.key + last;
        if (e.key === ' ') { // Prevent space straight away
            e.preventDefault();
        } else {
            if (getSelectionText() == oldVal) {
                // The old value was selected, make the entered key the newVal
                newVal = e.key;
            }
            if (
                // Allow following input
                ((e.key === '+' || e.key === '-') && newVal.substr(1).indexOf(e.key) == -1) ||
                (newVal.indexOf(e.key) === 0 && e.key === ',') ||
                (newVal.indexOf(e.key) === 0 && e.key === 'Decimal') || // For '.' in IE...
                (e.key === 'Backspace') ||
                (e.key === 'Tab') ||
                (e.key === 'Insert') ||
                (e.key ==='Delete') ||
                (e.key === 'ArrowLeft' || e.key === 'ArrowRight') ||
                (e.key === 'Home' || e.key === 'End') ||
                ((e.keyCode === 65 || e.keyCode === 67 || e.keyCode === 86) && (e.ctrlKey === true || e.metaKey === true)) // Ctrl+A/C/V, Command+A/C/V
            ) {
                return; // Let it happen
            } else if (isNaN(newVal.replace(',', '.'))) {
                // Not a number, block input
                e.preventDefault();
            } else if(newVal.indexOf(',') >= 0 && e.key != ',') { // Allow only 1 decimal
                var decimals = newVal.substr(newVal.indexOf(',') + 1);
                if (decimals.length > 1) {
                    e.preventDefault();
                }
            } else if(newVal.indexOf('.') >= 0 && e.key != '.') { // Allow only 1 decimal
                var decimals = newVal.substr(newVal.indexOf('.') + 1);
                if (decimals.length > 1) {
                    e.preventDefault();
                }
            }
        }           
        
    });  
}

Comments