Skip to content

Instantly share code, notes, and snippets.

@john-cheesman
Last active July 13, 2016 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john-cheesman/fbdd5359461e4678b6e462c9e62fc819 to your computer and use it in GitHub Desktop.
Save john-cheesman/fbdd5359461e4678b6e462c9e62fc819 to your computer and use it in GitHub Desktop.
Number Field jQuery plugin
(function ($) {
var allowedKeys;
// Allow left, right, tab, backspace, delete and escape
allowedKeys = [8, 9, 37, 39, 46, 27];
$.fn.numberField = function () {
return this.each(function () {
$(this).on('keypress', function (event) {
var character;
character = String.fromCharCode(event.which);
// Only permit allowed keys and 0-9 to be entered
if (allowedKeys.indexOf(event.keyCode) > -1 || character.match(/([0-9])+/g)) {
return true;
}
return false;
});
});
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment