Reputation: 16792
I have <input type="number">
and enter into the text field non-numeric characters just fine. Sure, I could use javascript to enforce this "number-only" requirement but is there an HTML5 way to do it in the HTML itself?
Demo:
(I'm using Google Chrome)
Upvotes: 1
Views: 1577
Reputation: 1038720
No, there's no pure HTML way to achieve that. You will need javascript if you want to prevent the user from typing arbitrary characters. HTML5 provides the pattern
attribute which will prevent the form from submitting if the user entered invalid value:
<form>
<input type="number" pattern="\d+" />
<input type="submit" />
</form>
Upvotes: 4