I have tried Regex accept numeric only. First character can't be 0 and What is the regex for "Any positive integer, excluding 0" however that didn't work as per my requirements.
I want exact 6 digit numeric number but shouldn't start with 0
I've tried
^[1-9][0-9]{6}*$
^([^0][0-9]){6}$
...
Need fine tuning.
The problem with ^[1-9][0-9]{6}*$
is it is an invalid regex because of {6}*
and ^([^0][0-9]){6}$
is that it is allowing any character that is not 0
followed by six digits.
Use
^[1-9][0-9]{5}$
Explanation:
^
: Starts with anchor [1-9]
: Matches exactly one digit from 1 to 9 [0-9]{5}
: Matches exactly five digits in the inclusive range 0-9
$
: Ends with anchor HTML5 Demo:
input:invalid { color: red; }
<input type="text" pattern="[1-9][0-9]{5}" />
Some websites and banks have the habit of spacing pincode after 3 digits. To match both 515411 and 515 411 the following pattern will help.
^[1-9]{1}[0-9]{2}\s{0,1}[0-9]{3}$
This regular expression covers;
PIN code doesn't start from zero
Allows 6 digits only
Allows 6 consecutive digits (eg 431602)
Allows 1 space after 3 digits (eg 431 602)
([1-9]{1}[0-9]{5}|[1-9]{1}[0-9]{3}\\s[0-9]{3})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.