If you want to limit input (as opposed to checking), you can work with key events. something like that:
<input type="text" class="numbersOnly" value="" />
and
jQuery('.numbersOnly').keyup(function () { this.value = this.value.replace(/[^0-9\.]/g,''); });
or , we can update this after the user βleftβ this field, with the on change event, so the user can navigate through the text using the arrow keys.
jQuery('.numbersOnly').on('change', function () { this.value = this.value.replace(/[^0-9\.]/g,''); });
This immediately allows the user to know that they cannot enter alpha characters, etc., and not later in the verification phase.
You still want to check, because the input can be filled out by cutting and pasting with the mouse or, possibly, using auto-completion of the form, which may not trigger key events.
Fiddle: http://jsfiddle.net/shannonhochkins/eu7P9/
source share