2

How do I pattern match validation for input of 1 to 3 digit number. I tried below and it does not work

<input type="text" title="FOO should be a 3 Digit Number Only" pattern="[\d]{3}" id="uid" name="foo<?php echo $i+1;?>" placeholder="Enter FOO" placeholder="FOO" required="'required'" minlength="1" maxlength="3" />

2 Answers 2

4

You are looking for a RegEx: /^\d{1,3}$/ or ^0*\d{1,3}$ to allow numbers starting with any amout of 0s.

<input type="text" pattern="^\d{1,3}$" />

^ makes sure the string starts with that pattern, and $ makes sure that it ends with that pattern

You can try your RegEx on websites like https://regex101.com or http://regexr.com

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

1 Comment

Works well. Thanks for the links too
0

Try to use this pattern:

pattern="^\d{1,3}$"

The other way is to use maxlength property like

<input type="text" maxlength="3" />

This will make sure that you can have maximum 3 digit number in your textbox.

2 Comments

you mean type="number", but that will also accept 0 digit numbers and numbers with dots
Rahul, the input does not work as you described! It just accepts any text with a length from 0 to 3

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.