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.
NOTE: If your webpage uses HTML5, you can use the built in The following is minimized and also allows for use of CTRL+x, CTRL+c, and CTRL+v
|
|||||||||||||||||||||
|
Here is the function I use:
You can then attach it to your control by doing:
|
|||||||||||||||||||||
|
Inline:
Unobtrusive style (with jQuery):
|
|||||||||||||||||||||
|
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. or for event keycode, simple use below :
|
|||||||||||||||||
|
You can use on input event like this:
But, what's this code privilege?
|
|||||||||||||||||
|
Use JavaScript function isNaN,
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 |
|||||||||||||||||
|
I use this in our internal common js file. I just add the class to any input that needs this behavior.
|
|||||
|
Simpler one for me is
|
|||||||||
|
Short and sweet - even if this will never find much attention after 30+ answers ;)
|
|||||||||
|
You can do the same by using this very simple solution
I referred to this link for the solution. It works perfectly!!! |
|||||||||
|
The pattern attribute in HTML5 specifies a regular expression that the element's value is checked against.
Note: The pattern attribute works with the following input types: text, search, url, tel, email, and password.
|
||||
|
You can try the HTML5 number input:
For non-compliant browsers there are Modernizr and Webforms2 fallbacks. |
|||||
|
Why so complicated? You don't even need jQuery because there is a HTML5 pattern attribute:
The cool thing is that it brings up a numeric keyboard on mobile devices, which is way better than using jQuery. |
|||||||||||||
|
Something fairly simple using jQuery.validate
|
||||
|
function suppressNonNumericInput(event){
|
||||
|
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:
|
|||||
|
I think it will help everyone
|
|||||
|
I came to a very good and simple solution that doesn't prevent the user from selecting text or copy pasting as other solutions do. jQuery style :)
|
|||||
|
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 ;) |
||||
|
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/
|
||||
|
Here is an answer that uses jQuery UI Widget factory. You can customize what characters are allowed easily.
That would allow numbers, number signs and dollar amounts.
|
|||||
|
Need to make sure you have the numeric keypad and the tab key working too
|
||||
|
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 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 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:
|
||||
|
This seems unbreakable.
|
|||||||||
|
Simple way to check that enter value is numeric is:
|
||||
|
Just need to apply this method in Jquery and you can validate your textbox to just accept number only.
Try this solution here |
||||
|
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?