Any quick way to set an HTML text input (<input type=text />
) to only allow numeric keystrokes(plus '.')?
|
||||
JavaScript (the most relibable still)
While this is simple, it will not let you use combination keys and other non typeable keys. For a more complete JavaScript solution that also support input of type number and max length validation, consider using this Polyfill. HTML 5 (does not require JavaScript, and also does not behave in standard way in many modern browsers.)
Try input type=number to see the HTML5 version in action. jQuery
More complex validation options If you want to do some other validation bits and pieces, this could be handy: http://www.javascript-coder.com/html-form/javascript-form-validation.phtml https://github.com/lockevn/html-numeric-input But don't forget you still must do server side validation! |
|||||||||||||||||||||
|
Use this DOM
And this script
|
|||||||||||||||||||||
|
I've searched long and hard for a good answer to this, and we desperately need
If you dislike the non-accepted character showing for a split-second before being erased, the method below is my solution. Note the numerous additional conditions, this is to avoid disabling all sorts of navigation and hotkeys. If anyone knows how to compactify this, let us know!
|
|||||||||||||||||||||
|
HTML5 has |
||||
|
I opted to use a combination of the two answers mentioned here i.e.
and
|
|||||
|
And one more example, which works great for me:
Also attach to keypress event
And html:
Update: |
||||
|
2 solutions: Use a form validator (for example with jQuery validation plugin) Do a check during the onblur (i.e. when the user leaves the field) event of the input field, with the regular expression:
|
|||||
|
HTML5 supports regexes, so you could use this:
Warning: Some browsers don't support this yet. |
|||||||||
|
so simple.... // In Javascript Function (can use HTML or PHP).
In Your Form Input :
With input max. (these above, is allow for 12 Digit number) |
||||
|
Most answers here all have the weakness of using key- events. Many of the answers would limit your ability to do text selection with keyboard macros, copy+paste and more unwanted behavior, others seem to depend on specific jQuery plugins, which is killing flies with machineguns. This simple solution seems to work best for me cross platform, regardless of input mechanism (keystroke, copy+paste, rightclick copy+paste, speech-to-text etc.). All text selection keyboard macros would still work, and it would even limit ones ability to set a non-numeric value by script.
|
||||
|
JavaScript
additional you could add comma, period and minus (,.-)
HTML
|
||||
|
A short and sweet implementation using jQuery and replace() instead of looking at event.keyCode or event.which:
Only small side effect that the typed letter appears momentarily and CTRL/CMD + A seems to behave a bit strange. |
||||
|
If you want to suggest to the device(maybe a mobile phone) between alpha or numeric you can use |
||||
|
if you just want to only allow numbers 0-9 on a input, you can just remove all non numeric characters, without messing about charCodes and arrows ctrl and others kinds of keyboars, and this fix a string pasted or draged to a input. this have a advantage that you don't have to worry about browser messy of keyEvents. you can use this to allow letters and -,. but this cant deal with valid numbers like (0.1.4- is not a integer nor float), in this case this others guys solutions is better.
Javascript
yes, regex is less work but not faster than this. |
||||
|
|
||||
|
In other case this will help you.
|
||||
|
Just an other variant with jQuery using
|
||||
|
This is the extended version of geowa4's solution. Supports Usage:
If the inputs are dynamic use this:
|
||||
|
this is an improved function :
|
||||
|
Javascript code:
HTML code:
works perfectly because backspace keycode is 8 and regex expression doesnt let it so its a easy way to bypass the bug :) |
||||
|
The best way (allow ALL type of numbers - real negative, real positive, iinteger negative, integer positive) is:
|
||||
|
How about this DOM...
...and this script?
...OR this script, without indexOf, using 2 for's...
I used the onkeydown attribute instead of onkeypress because the onkeydown attribute is checked before onkeypress attribute. The problem would be in the google chrome browser. With the attribute "onkeypress", TAB would be uncontrollable with "preventDefault" on google chrome, however, with the attribute "onkeydown", TAB becomes controllable! ASCII Code for TAB => 9 The first script have less code than the second, however, the array of ASCII characters must have all the keys. The second script is much bigger than the first, but the array does not need all keys, the first digit in each position of the array is the number of times each position will be readed. For each reading, will be incremented 1 to the next one. For example:
48 + NCount = 48 NCount + + 48 + NCount = 49 NCount + + ... 48 + NCount = 57
ASCII Codes:
I'm gonna sleep now!! This is my first post on StackOverflow :P |
||||
|
you can use pattern for this. here you can see the complete mobile website interface tips |
||||
|
Lots of great answers here but thought I'd contribute this simple one which allows for exactly one decimal but no more:
|
||||
|
Remember the regional differences (Euros use periods and commas in the reverse way as Americans), plus the minus sign (or the convention of wrapping a number in parentheses to indicate negative), plus exponential notation (I'm reaching on that one). |
||||
|
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:
|
||||
|
You can also compare input value (which is treated as string by default) to itself forced as numeric, like:
However, you need to bind that to event like keyup etc. |
||||
|
You can replace the Shurok function with:
|
||||
|
I tweaked it some, but it needs a lot more work to conform to the JavaScript weirding way.
And then it's called via:
|
||||
|
|
||||
|
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