I am creating a web page where I have an input text field in which I want to allow only numeric characters like (0,1,2,3,4,5...9) 0-9.
How can I do this using jQuery?
I am creating a web page where I have an input text field in which I want to allow only numeric characters like (0,1,2,3,4,5...9) 0-9. How can I do this using jQuery? |
|||||||||||||
|
Have a look at this plug-in (TexoTela jQuery numeric). This (jStepper) is another one. This is a link if you want to build it yourself.
|
|||||||||||||||||||||
|
Here is the function I use:
You can then attach it to your control by doing:
|
|||||||||||||||||||||
|
You could just use a simple JavaScript regular expression to test for purely numeric characters:
This returns true if the input is numeric or false if not. |
|||||
|
Inline:
Unobtrusive style:
|
||||
|
Not using jQuery but be simple and just use one JavaScript function. Use JavaScript function isNaN,
@GalacticCowboy
Update: And here a nice article talking about it but using jQuery: Restricting Input in HTML Textboxes to Numeric Values |
|||||||||||||
|
Source: http://snipt.net/GerryEng/jquery-making-textfield-only-accept-numeric-values |
|||||||||
|
You can use this JavaScript function:
And you can bind it to your textbox like this:
I use the above in production, and it works perfectly, and it is cross-browser. Furthermore, it does not depend on jQuery, so you can bind it to your textbox with inline JavaScript:
|
||||
|
Simpler one for me is
|
||||
|
|
||||
|
I use this in our internal common js file. I just add the class to any input that needs this behavior.
|
||||
|
Something fairly simple using jQuery.validate
|
||||
|
I think it will help everyone
|
||||
|
There is an incredible compatibility issue with using keystrokes to detect the character pressed... see quirksmode to know more about that. I would suggest using keyup to create your filter because then you have the $(element).val() method you can use to evaluate actual universal characters. Then you can filter out any NON digits using a regex like: replace(/[^0-9]/g,''); This takes care of all issues like shift and paste problems because there is always a keyup and so the value will always be evaluated (unless javascript is turned off). So... to turn this into JQuery... Here is a little unfinished plugin I'm writing, it is called inputmask and will support more masks when finished. For now it has the digits mask working. Here it goes...
To use it just do:
The 'someOtherNotYetImplementedValidator' is just there to show how this can be expanded for extra future masks/validators. You can add it or leave it out, it doesn't break anything ;-) Appologies for the extra clutter of comments, I'm using a template I created for the guys here at work. Hope this helps, Cheers |
||||
|
I wanted to help a little, and I made my version, the
Just add in input tag "...input ... id="price" onkeydown="return onlyNumbers(event)"..." and you are done ;) |
||||
|
I also would like to answer :)
That's it...just numbers. :) Almost it can work just with the 'blur', but... |
||||
|
You would want to allow tab:
|
||||
|
Need to make sure you have the numeric keypad and the tab key working too
|
||||
|
I wrote mine based off of @user261922's post above, slightly modified so you can select all, tab and can handle multiple "number only" fields on the same page.
|
||||
|
I had a problem with the top-answer. It doesn't include the numerical keypad and if one presses shift+number the special-signs shouldn't be displayed either.. but this solution doesn't take care of it. The best link I've found in this thread was this: http://www.west-wind.com/weblog/posts/2011/Apr/22/Restricting-Input-in-HTML-Textboxes-to-Numeric-Values I'm new to stackoverflow so I don't know if I can just edit the better solution into the top-post. |
||||
|
You can try the HTML5 number input:
For non-compliant browsers there are Modernizr and Webforms2 fallbacks. |
||||
|
This is why I recently wrote to accomplish this. I know this has already been answered but I'm leaving this for later uses. This method only allows 0-9 both keyboard and numpad, backspaces, tab, left and right arrows (normal form operations)
|
||||
|
this one was very helpful http://jstepper.emkay.dk/Default.aspx the other ones accepts languages different than English |
||||
|
The above $ plugin is written from the AjaxControlToolkit filter textbox extender.js. However one behavior is not borrowed from the AjaxControlToolkit is that when the user copies and pastes any non-numeric value then the onchange event fires up and text box eats up the values. I went through the code and found out for this onchange was called after every 250ms, which is a performance hit, hence commented that part. |
||||
|
You could also use this jQuery plugin, jQuery alphaNumeric. I found it to be really great. |
||||
|
It may be overkill for what you are looking for, yet I suggest a jQuery plugin called autoNumeric() - it is great! You can limit to only numbers, decimal precision, max / min values and more. |
||||
|
Here is a quick solution I created some time ago. you can read more about it in my article: http://ajax911.com/numbers-numeric-field-jquery/
|
||||
|
To elaborate a little more on answer #3 I'd do the following (NOTE: still does not support paste oprations through keyboard or mouse):
|
||||
|
This (jQuery numeric plugin) one works fine and has some neat features but you might have to adjust it a little bit for your needs and IE support. |
||||
|
I recommend to check event.metaKey as well. If that's set to true, the user might be doing something like cmd-A to select all the text in the field. You should allow that too. |
||||
|
I'm using in this form. Seems correct to me allow keys like home, end, shift and ctrl, with the drawback of the user to can print special chars:
|
||||
|
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.