Premlatha
Premlatha

Reputation: 1976

How to add multiple pattern in html input text

How to use multiple pattern in html text input. So that,user can input 1 digit, 2 digit or 3 digit numbers.

1:add '|' between each pattern

<input type="text" name="id" id="id" maxlength="3" pattern="\d{1}|\d{2}|\d{3}" required>

Result: Able to input one digit number only

2: add '|' between each pattern and bracket for each pattern

<input type="text" name="id" id="id" maxlength="3" pattern="(\d{1})|(\d{2})|(\d{3})" required>

Result: Able to input one digit number only

Upvotes: 0

Views: 1133

Answers (1)

Justinas
Justinas

Reputation: 43481

Use range regex instead of exact number match. So in your case it would be \d{1,3}

In RegEx there is range selector {m,n}.

From 1 to 3: \d{1,3}
From 2 to any: \d{2,}

Upvotes: 1

Related Questions