candlejack
candlejack

Reputation: 1209

HTML5 input pattern - exactly 'N' quantity of numbers

I want an input that it can only enter exactly N quantity of numbers , no less, no more.

I have tried this: Example with 4 digits.

<input type="text" name="myinput" pattern="[0-9]{4}" title="Only four (4) digits.">

But it does not work, someone knows how to fix it?

Upvotes: 0

Views: 1798

Answers (1)

JJJ
JJJ

Reputation: 33163

By "it does not work" I assume you mean the pattern isn't checked when the input is empty. Otherwise there doesn't seem to be anything wrong with the pattern.

That is how the pattern attribute works. You'll have to add required as well if you don't want to allow empty input.

<form>
    <input type="text" name="myinput" pattern="[0-9]{4}" required>

    <button type="submit">Submit</button>
</form>

Upvotes: 2

Related Questions