What is the best way to restrict "number"-only input for textboxes?
I am looking for something that allows decimal points.
I see a lot of examples. But have yet to decide which one to use.
Update from Praveen Jeganathan
No more plugins, jQuery has implemented its own jQuery.isNumeric()
added in v1.7.
See: https://stackoverflow.com/a/20186188/66767
And apply this on all the inputs you want:
The best way is to check the contects of the text box whenever it loses focus.
You can check whether the contents are a "number" using a regular expression.
Or you can use the Validation plugin, which basically does this automatically.
/* this is my cross browser version of How to allow only numeric (0-9) in HTML inputbox using jQuery?
*/
If you're using HTML5 you don't need to go to any great lengths to perform validation. Just use -
The step attribute allows the decimal point to be valid.
The jquery.numeric plugin has some bugs that I notified the author of. It allows multiple decimal points in Safari and Opera, and you can't type backspace, arrow keys, or several other control characters in Opera. I needed positive integer input so I ended up just writing my own in the end.
Check this find code for Database use: