2

I need to have an input field where the user can only enter 7 numbers, nothing more, nothing less, nothing else.

I need (check for 7 characters)

pattern=".{7,7}"

and (check for only numbers)

pattern="\d*"

working together, but you can't define pattern two times, how would I combine these two together?

2 Answers 2

2

You may use

pattern="\d{7}"

or with explicit anchors:

pattern="^\d{7}$"

See a regex demo.

You do not need to specify identical values for the minimum and maximum thresholds. In case you want to allow 1 to 7 digits, then it makes sense: pattern="\d{1,7}".

input:valid {
  color: black;
}
input:invalid {
  color: navy;
}
<form name="form1"> 
 <input type="text" pattern="\d{7}" title="7 digits only!" required/>
 <input type="Submit"/> 
</form>

To make the input field obligatory, add the required attribute.

Sign up to request clarification or add additional context in comments.

2 Comments

One more question: if you input nothing, the form still sends, what can I add to stop this?
@MatthiasVerhoeven: Use the required attribute.
0

Try this code

 pattern="[0-9]{7}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.