neubert
neubert

Reputation: 16792

html5 way to make input type="number" only accept numbers?

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:

http://jsfiddle.net/dqev8qmz/

(I'm using Google Chrome)

Upvotes: 1

Views: 1577

Answers (1)

Darin Dimitrov
Darin Dimitrov

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

Related Questions