Input filter showcase

Text input filters with jQuery.

by KarmaProd

HTML

<h2>jQuery input filter showcase</h2>
<p>Supports Copy+Paste, Drag+Drop, keyboard shortcuts, context menu operations, non-typeable keys, the caret position, different keyboard layouts, and <a href="https://caniuse.com/#feat=input-event" target="_blank">all browsers since IE 9</a>.</p>
<p>There is also a <a href="https://jsfiddle.net/emkey08/zgvtjc51" target="_blank">pure JavaScript version</a> of this (without jQuery).</p>
<table>
  <tr><td>Integer</td><td><input id="intTextBox"></td></tr>
  <tr><td>Integer &gt;= 0</td><td><input id="uintTextBox"></td></tr>
  <tr><td>Integer &gt;= 0 and &lt;= 500</td><td><input id="intLimitTextBox"></td></tr>
  <tr><td>Float (use . or , as decimal separator)</td><td><input id="floatTextBox"></td></tr>
  <tr><td>Currency (at most two decimal places)</td><td><input id="currencyTextBox"></td></tr>
  <tr><td>A-Z only</td><td><input id="latinTextBox"></td></tr>
  <tr><td>Hexadecimal</td><td><input id="hexTextBox"></td></tr>
</table>

CSS

.input-error{
    outline: 1px solid red;
}

JavaScript

// Restricts input for each element in the set of matched elements to the given inputFilter.
(function($) {
  $.fn.inputFilter = function(callback, errMsg) {
    return this.on("input keydown keyup mousedown mouseup select contextmenu drop focusout", function(e) {
      if (callback(this.value)) {
        // Accepted value
        if (["keydown","mousedown","focusout"].indexOf(e.type) >= 0){
          $(this).removeClass("input-error");
          this.setCustomValidity("");
        }
        this.oldValue = this.value;
        this.oldSelectionStart = this.selectionStart;
        this.oldSelectionEnd = this.selectionEnd;
      } else if (this.hasOwnProperty("oldValue")) {
        // Rejected value - restore the previous one
        $(this).addClass("input-error");
        this.setCustomValidity(errMsg);
        this.reportValidity();
        this.value = this.oldValue;
        this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
      } else {
        // Rejected value - nothing to restore
        this.value = "";
      }
    });
  };
}(jQuery));


// Install input filters.
$("#intTextBox").inputFilter(function(value) {
  return /^-?\d*$/.test(value); }, "Must be an integer");
$("#uintTextBox").inputFilter(function(value) {
  return /^\d*$/.test(value); }, "Must be an unsigned integer");
$("#intLimitTextBox").inputFilter(function(value) {
  return /^\d*$/.test(value) && (value === "" || parseInt(value) <= 500); }, "Must be between 0 and 500");
$("#floatTextBox").inputFilter(function(value) {
  return /^-?\d*[.,]?\d*$/.test(value); }, "Must be a floating (real) number");
$("#currencyTextBox").inputFilter(function(value) {
  return /^-?\d*[.,]?\d{0,2}$/.test(value); }, "Must be a currency value");
$("#latinTextBox").inputFilter(function(value) {
  return /^[a-z]*$/i.test(value); }, "Must use alphabetic latin characters");
$("#hexTextBox").inputFilter(function(value) {
  return /^[0-9a-f]*$/i.test(value); }, "Must use hexadecimal characters");