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
You can use pattern for this:
Here you can see the complete mobile website interface tips.
HTML5 supports regexes, so you could use this:
Warning: Some browsers don't support this yet.
Just an other variant with jQuery using
Here is a simple one which allows for exactly one decimal, but no more:
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:
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.