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? |
|||||||||||||||||||||
|
You can try the HTML5 number input:
This input tag element would now take value only between 0 to 9 as min attribute is set to 0 and max attribute is set to 9. for more information on visit http://www.w3schools.com/html/html_form_input_types.asp |
|||||||||
|
To elaborate a little more on answer #3 I'd do the following (NOTE: still does not support paste oprations through keyboard or mouse):
|
||||
|
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. |
||||
|
If you have to solve diacritics and special characters, try to use this:
|
||||
|
Here is way with regular expression:
}); https://jsfiddle.net/astrapi69/qbk2vjty/1/ And you can change the regular expression to anything else if you want to restrict other characters then numbers. |
|||||
|
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. |
||||
|
You can use HTML5 input type number to restrict only number entries:
This will work only in HTML5 complaint browser. Make sure your html document's doctype is:
For general purpose, you can have JS validation as below:
If you want to allow decimals replace the "if condition" with this:
Source: HTML Text Input allow only Numeric input JSFiddle demo: http://jsfiddle.net/Gagan_Gami/nSjy7/333/ |
||||
|
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:
|
|||||
|
Use the jquery numeric value. Below function allows for decimal and numeric values. |
||||
|
Another approach is below. This will take care of pasting also. [it is for alpha-numeric validation]
|
||||
|
This jQuery code filters out characters typed while Shift, Ctrl or Alt is held down.
|
||||
|
|
||||
|
Check if decimal point already used:-
|
||||
|
|
||||
|
Not moving towards answer, i used accepted, but here is an implementation with some added logic to format the number in real time |
||||
|
This answer was perfect, but we can even make it better and more powerful by combining it with the jQuery.Validation plugin. By using the number() method, we can develop something like this:
So, any Textbox in the "#myFormId" form, with "numberOnly" class, accept only number including decimal, float, and even negative number. Voila :) PS: In my case, for some reason I used jQuery.validator.addMethod() instead of .validate():
(it works fine inside my ASP.NET MVC 3 project + unobtrusive JavaScript validation, hooooooooray!) |
||||
|
Refactored the accepted answer so that comments no longer need to used because I hate comments. Also this is easier to test with jasmine.
|
||||
|
add below code in document.ready
|
||||
|
Use below simple jQuery to allow only numeric characters in a tetbox. You do not need to filter all the special characters manually so there is no danger of missing some special char. This will allow only numbers 0-9: (Place below code in document ready and change the class name as per your numeric text fields class name.)
|
||||
|
I have combined all the answers in one and come up with the following code:
In addition the form should have |
||||
|
put a class in input text and name it only_numbers put the jquery code in the page
have fun :-) |
||||
|
Try this one
|
||||
|
|
|||||
|
I hope this will work on all browsers. |
||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?