Last active
July 13, 2016 10:03
-
-
Save john-cheesman/fbdd5359461e4678b6e462c9e62fc819 to your computer and use it in GitHub Desktop.
Number Field jQuery plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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