jQuery: Only allow user to enter numbers

Here's a nice little snippet for blocking all other key presses than numerical and backspace:
$('#selector').bind('keypress', function(e) {
return !( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
}

(#selector is the selector to your input field/fields to apply this filter. For info on how selectors works in jQuery please visit the official documentation about selectors.)

Related posts:

Comments

comments powered by Disqus