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
This is the extended version of geowa4's solution. Supports
min
andmax
attributes. If the number is out of range, the previous value will be shown.You can test it here.
Usage:
<input type=text class='number' maxlength=3 min=1 max=500>
If the inputs are dynamic use this:
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).
Use this DOM
And this script
I opted to use a combination of the two answers mentioned here i.e.
<input type="number" />
and
<input type="text" onkeypress="return isNumberKey(event);">
HTML5 has
<input type=number>
, which sounds right for you. Currently, only Opera supports it natively, but there is a project that has a JavaScript implementation.