Input filter showcase

Text input filters with jQuery.

by hcanning2012

HTML

Integer<input class="tuitionInput"/>
Integer<input class="tuitionInput"/>

JavaScript

// Restricts input for each element in the set of matched elements to the given inputFilter.

  $.fn.inputFilter = function(inputFilter) {
    return this.on("input keydown keyup mousedown mouseup select contextmenu drop", function() {
      if (inputFilter(this.value)) {
        this.oldValue = this.value;
        this.oldSelectionStart = this.selectionStart;
        this.oldSelectionEnd = this.selectionEnd;
      } else if (this.hasOwnProperty("oldValue")) {
        this.value = this.oldValue;
        this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
      } else {
        this.value = "";
      }
    });
  };




$(".tuitionInput").inputFilter(function(value) {
  return /^\d*$/.test(value); });