/ Published in: JavaScript
Expand |
Embed | Plain Text
$('input.valid-number').bind('keypress', function(e) { return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ; })
Comments
Subscribe to comments
You need to login to post a comment.

This code won't catch the alternate values on the numeric keys. For example, #, ! and @ will all be allowed as the key pressed for these is still the number key.
I changed the code to account for this as follows:
$('input.valid-number').bind('keypress', function(e) { return (e.which === 8 || e.which === 0 || (e.shiftKey === false && (e.which > 47 && e.which < 58))) ? true : false; }
Forgive the lack of formatting, this was my first post and I'm not used to Markdown.