Is there a quick way to set an HTML text input (<input type=text />
) to only allow numeric keystrokes (plus '.')?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
And one more example, which works great for me:
Also attach to keypress event
And HTML:
Here is a jsFiddle example
input type="number"
is an HTML5 attribute.In the other case this will help you:
A safer approach is checking the value of the input, instead of hijacking keypresses and trying to filter keyCodes.
This way the user is free to use keyboard arrows, modifier keys, backspace, delete, use non standard keyboars, use mouse to paste, use drag and drop text, even use accessibility inputs.
The below script allows positive and negative numbers
EDIT: I removed my old answer because I think it is antiquated now.
Please find below mentioned solution. In this user can be able to enter only
numeric
value, Also user can not be able tocopy
,paste
,drag
anddrop
in input.Let me know if it not works.
One more example where you can add only numbers in the input field, can not letters
just use type="number" now this attribute supporting in most of the browsers