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
This is very simple that we have already a javascript inbuilt function "isNaN" is there.
You can use the Validation plugin with its number() method.
As a slight improvement to this suggestion, you can use the Validation plugin with its number(), digits, and range methods. For example, the following ensures you get a positive integer between 0 and 50:
I first tried solving this using jQuery, but I wasn't happy about unwanted characters (non-digits) actually appearing in the input field just before being removed on keyup.
Looking for other solutions I found this:
Integers (non-negative)
Source: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onkeypress#Example Live example: http://jsfiddle.net/u8sZq/
Decimal points (non-negative)
To allow a single decimal point you could do something like this:
Source: Just wrote this. Seems to be working. Live example: http://jsfiddle.net/tjBsF/
Other customizations
Some things you could be interested in doing:
Shortcomings
I know the title asks for jQuery solutions, but hopefully someone will find this useful anyway.
Thanks for the post Dave Aaron Smith
I edited your answer to accept decimal point and number's from number section. This work perfect for me.
This function does the same thing, uses some of the ideas above.