skip to content »

Javascript for validating numbers in textbox

Added numeric keypad and allow for decimal/thousand separator. Lot of comments that this won't solve all numeric entry problems and that's very true - it's not meant to. You will still need to validate the final input for proper formatting etc. Nice piece of work - as you say, it's not a validator, but an entry [email protected] - sometimes, when users & the readers of form entries aren't computer literate, we do need to restrict what people can type, to limit [email protected] - I recommend you check out my key code checker which makes it easy to find the key codes to block or perform special actions for.

javascript for validating numbers in textbox-59javascript for validating numbers in textbox-85

This method returns true if it finds a match, otherwise it returns false.So I took a few minutes to write a small reusable plug-in that handles this scenario.Imagine you have a couple of textboxes on a form like this: The plug-in basically goes through each selected element and hooks up a keydown() event handler.Somebody asked me this today on a support call so I did a few quick lookups online and found the solutions listed rather unsatisfying.The main problem with most of the examples I could dig up was that they only include numeric values, but that provides a rather lame user experience. Web Toolkit/samples/Ajax/html5and Css3/Check for keycode 67 and Key set inside of keydown: This is interesting javascript, I learned a lot of things, thanks, but there is a *big* problem: on French keyboards (and probably for many other countries as well), you have to press Shift in order to type digits.

So your plugin actually allows French users to type things like &é"' but they cannot type any digits at all.

The example page is at Yes this doesn't work for *validating* numeric input, so if you paste this doesn't do the trick.

But that's not the focus of this little plug-in - that's validation plug-ins are for.

(using IE 7.0.6) I just put above advice into the code.

As far as i know IE does not have any problems with regular expressions at all. @mathewgk80 For allowing only numeric values to be placed inside a text box using Java Script, we need to capture the “on Key Down” event for the keyboard.

This is very helpful when testing for Username, password, dollar amounts, etc.