Created
September 19, 2012 06:20
-
-
Save colourgarden/3747989 to your computer and use it in GitHub Desktop.
Ensure that the character entered is numeric. Useful for checkout quantity fields.
This file contains 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
$('input').keydown(function(e){ | |
// Make some allowances for: delete, backspace, tab and escape | |
if ( e.keyCode == 46 || e.keyCode == 8 || e.keyCode == 9 || e.keyCode == 27 || | |
// Allow: Ctrl+A | |
( e.keyCode == 65 && e.ctrlKey === true ) || | |
// Allow: home, end, left, right | |
( e.keyCode >= 35 && e.keyCode <= 39 )) { | |
// Let it happen, don't do anything | |
return; | |
} | |
else { | |
// Ensure that it's a number else stop the keypress | |
if (e.shiftKey || (e.keyCode < 48 || e.keyCode > 57) && (e.keyCode < 96 || e.keyCode > 105 )) { | |
e.preventDefault(); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment