Any quick way to set an HTML text input (<input type=text />
) to only allow numeric keystrokes(plus '.')?
Join them; it only takes a minute:
|
||||
You can attach to the key down event and then filter keys according to what you need, for example:
And the actual javascript handler would be:
|
|||||||||
|
I tweaked it some, but it needs a lot more work to conform to the JavaScript weirding way.
And then it's called via:
|
|||||||||
|
This removes any bad character instantly, allows only 1 dot, is short and allows backspace etc.
|
||||
|
Code bellow will also check for PASTE event.
|
||||
|
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. |
|||||
|
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.
|
||||
|
I might have another (simple) workaround for this .. Since String.fromCharCode(key) returns weird things upon azerty keyboard ( numerical keypad returns code as g for 1, and 1 for & caracter .. I've realized catching the final value on keyup within the input to reset it to an arbitrary value is a simpler, lightweight & bugproof method ( could also be done via some regex ... to keep decimals and so on ... don't have to filter other ctrl, home, del, enter events anymore .. ) Usage with jq :
Onkeyup attribute :
|
||||
|
When it comes to fool-proofing UX, one should always try to keep a reference point for the 'user's intelligence'. While neglecting everything other than numbers, a dot and a hyphen would seem like the perfect choice, you should also consider letting them enter any content, and when they're done, purify the input; if not a valid number, show error. This method would make sure no matter what the user manages to do, the result will always be valid. If the user is naive enough not to understand the warnings and error messages, pressing a button and seeing that nothing happens (as in keycode comparison) will only confuse him/her more. Also, for forms, validation and error message display are almost a necessity. So, the provisions might already be there. Here's the algorithm:
This method also gives you the added advantage of performing validations based on minimum value, maximum value, decimal places, etc if necessary. Just have to do these operations on the result after step 1.2. Disadvantages:
Advantages: Assuming the user have basic knowledge to read and understand,
|
||||
|
in html 5 ; |
||||
|
Thanks guys this really help me! I found the perfert one really useful for database.
Then add the eventhandler:
|
||||
|
I personally suggest to use the autoNumeric plugin from http://www.decorplanit.com/plugin/ - it supports all different variations like prefix/suffix handling, currency handling, negative value formatting, min, max etc. |
||||
|
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 (the association bonus does not count).
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