Skip to main content
Sorry, forgot to paste error message parameter. JS fiddle was already ok.
Source Link
Kar.ma
  • 855
  • 8
  • 14
$(document).ready(function() {
  $("#myTextBox").inputFilter(function(value) {
    return /^\d*$/.test(value);    // Allow digits only, using a RegExp
  },"Only digits allowed");
});
$(document).ready(function() {
  $("#myTextBox").inputFilter(function(value) {
    return /^\d*$/.test(value);    // Allow digits only, using a RegExp
  });
});
$(document).ready(function() {
  $("#myTextBox").inputFilter(function(value) {
    return /^\d*$/.test(value);    // Allow digits only, using a RegExp
  },"Only digits allowed");
});
Added CSS, I forgot about it.
Source Link
Kar.ma
  • 855
  • 8
  • 14

Apply your preferred style to input-error class. Here's a suggestion:

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

See the JSFiddle demo for more input filter examples. Also note that you still must do server side validation!

See the JSFiddle demo for more input filter examples. Also note that you still must do server side validation!

Apply your preferred style to input-error class. Here's a suggestion:

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

See the JSFiddle demo for more input filter examples. Also note that you still must do server side validation!

Added validity error message. Widely tested. Updated JS Fiddle accordingly
Source Link
Kar.ma
  • 855
  • 8
  • 14

Try it yourself on JSFiddleon JSFiddle.

There is no native jQuery implementation for this, but you can filter the input values of a text <input> with the following inputFilter plugin (supports Copy+Paste, Drag+Drop, keyboard shortcuts, context menu operations, non-typeable keys, the caret position, different keyboard layouts, validity error message, and all browsers since IE 9):

// Restricts input for the set of matched elements to the given inputFilter function.
(function($) {
  $.fn.inputFilter = function(inputFiltercallback, errMsg) {
    return this.on("input keydown keyup mousedown mouseup select contextmenu drop"drop focusout", function(e) {
      if (inputFiltercallback(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));

See the JSFiddle demoJSFiddle demo for more input filter examples. Also note that you still must do server side validation!

Try it yourself on JSFiddle.

There is no native jQuery implementation for this, but you can filter the input values of a text <input> with the following inputFilter plugin (supports Copy+Paste, Drag+Drop, keyboard shortcuts, context menu operations, non-typeable keys, the caret position, different keyboard layouts, and all browsers since IE 9):

// Restricts input for the set of matched elements to the given inputFilter function.
(function($) {
  $.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 = "";
      }
    });
  };
}(jQuery));

See the JSFiddle demo for more input filter examples. Also note that you still must do server side validation!

Try it yourself on JSFiddle.

There is no native jQuery implementation for this, but you can filter the input values of a text <input> with the following inputFilter plugin (supports Copy+Paste, Drag+Drop, keyboard shortcuts, context menu operations, non-typeable keys, the caret position, different keyboard layouts, validity error message, and all browsers since IE 9):

// Restricts input for the set of matched elements to the given inputFilter function.
(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));

See the JSFiddle demo for more input filter examples. Also note that you still must do server side validation!

Improve code; refer to JSFiddle for additional input filters which the OP didn't ask for
Source Link
emkey08
  • 6.5k
  • 5
  • 40
  • 39
Loading
Simplify answer (again)
Source Link
emkey08
  • 6.5k
  • 5
  • 40
  • 39
Loading
Simplify answer
Source Link
emkey08
  • 6.5k
  • 5
  • 40
  • 39
Loading
Cosmetics
Source Link
emkey08
  • 6.5k
  • 5
  • 40
  • 39
Loading
Keep answer concise
Source Link
emkey08
  • 6.5k
  • 5
  • 40
  • 39
Loading
Added more input filters, extended description
Source Link
emkey08
  • 6.5k
  • 5
  • 40
  • 39
Loading
Add note about updated answer
Source Link
emkey08
  • 6.5k
  • 5
  • 40
  • 39
Loading
Enhanced input filters and HTML 5 section
Source Link
emkey08
  • 6.5k
  • 5
  • 40
  • 39
Loading
Added link to pure JavaScript version of this answer
Source Link
emkey08
  • 6.5k
  • 5
  • 40
  • 39
Loading
Implement as plugin instead of function; fixed cosmetics
Source Link
emkey08
  • 6.5k
  • 5
  • 40
  • 39
Loading
Even more simplified code
Source Link
emkey08
  • 6.5k
  • 5
  • 40
  • 39
Loading
Bold major edit, but there is a *much* simpler solution for this - see JSFiddle
Source Link
emkey08
  • 6.5k
  • 5
  • 40
  • 39
Loading
Don't allow DOT .
Source Link
Prince Patel
  • 3.1k
  • 1
  • 24
  • 30
Loading
added 31 characters in body
Source Link
K.Dᴀᴠɪs
  • 10.1k
  • 13
  • 36
  • 46
Loading
Updated minified snippet to allow Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+X and prevent the letters: "acvx"
Source Link
Sylvan D Ash
  • 1.1k
  • 14
  • 24
Loading
update of broken link
Source Link
Braian Mellor
  • 2k
  • 3
  • 31
  • 51
Loading
use === instead of == and no spaces inside this parentheses
Source Link
Alexey B.
  • 12k
  • 2
  • 51
  • 76
Loading
Add both a minimized version and a more versatile use of answer. Also added script for testing on answer. Question is already polluted with answer. I saw no point in just making a new one.
Source Link
SpYk3HH
  • 22.6k
  • 11
  • 73
  • 81
Loading
e.ctrlkey does not support Mac Command+A, added e.metaKey to allow Select All from Mac
Source Link
William Isted
  • 12.4k
  • 4
  • 34
  • 47
Loading
Allowed up button
Source Link
Muxa
  • 5.7k
  • 6
  • 50
  • 57
Loading
Added in keycode 110 for keypad decimal.
Source Link
mawburn
  • 2.4k
  • 4
  • 31
  • 48
Loading