Any quick way to set an HTML text input (<input type=text />
) to only allow numeric keystrokes(plus '.')?
|
||||
another easy way with jquery:
now just set your each inputs class to Numeric, like:
|
||||
|
This removes any bad character instantly, allows only 1 dot, is short and allows backspace etc.
|
||||
|
You may try using the '''onkeydown''' event and cancel the event (event.preventDefault or something like that) when it's not one of the allowed keys. |
||||
|
I realize an old post but i thought this could help someone. Recently I had to limit a text box to just 5 decimal places. In my case ALSO the users input had to be less than 0.1
Here is the doCheck function
Here is the same function except to force integer input
hope that helps someone |
||||
|
Call this function when ready to validate what ever. I used a textbox here In my HTML:
In my javascript:
|
||||
|
Regular expressions and the match function can work well for this situation. For instance, I used the following to validate 4 input boxes that served as coordinates on a graph. It works reasonably well.
|
||||
|
and in the element
|
||||
|
Yes, HTML5 does. Try this code (w3school):
|
||||
|
I use the jquery.inputmask.js library you can download from NUGET. More specifically I use jquery.inputmask.regex.extensions.js that comes with it. I give the input element a class, in this case reg;
and then in javascript I set the mask;
This is for digits only but you can alter regex to accept "." By using this it is impossible to enter characters that are not digits. It is useful to have these inputmask libraries for general formatting. |
||||
|
If you are okay with using Plugins, here is one I tested. Works well except for paste. Here is a Demo http://jsfiddle.net/152sumxu/2 Code below (Lib pasted in-line) Demo of the plug-in Any Number
|
||||
|
And if you wanna update value after press key, you can change onChange for onKeypress, onKeyDown or onKeyup. But event onKeypress don't running in any browsers. |
||||
|
Code bellow will also check for PASTE event.
|
||||
|
For those of you that like one-liners.
I use this code on an input type="text", and with Angularjs to activate on keypress, but you can use jquery if like. Just put this code into a function that activates on keypress someway. Only allows digits, digits + decimal, digits + decimal + digits. CODE
This was built for US currency, but can be changed to allow more than two decimals past first decimal place as in the following... CHANGED CODE
:) |
||||
|
I was looking for a way to block an input of numbers, then, as I did not find it in answers, this code worked fine for me. I just need to input It in the onkeypress event. If you need just to block an input of numbers, I believe this will work fine.
|
||||
|
Thanks guys this really help me! I found the perfert one really useful for database.
Then add the eventhandler:
|
||||
|
protected by Community♦ Jun 17 '14 at 15:44
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.
Would you like to answer one of these unanswered questions instead?
<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57'></input>
– Droogans Jan 20 '13 at 20:13