Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
160
rated 0 times [  166] [ 6]  / answers: 1 / hits: 70893  / 9 Years ago, mon, november 23, 2015, 12:00:00

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.


More From » regex

 Answers
13

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:




  1. ^: Starts with anchor

  2. [1-9]: Matches exactly one digit from 1 to 9

  3. [0-9]{5}: Matches exactly five digits in the inclusive range 0-9

  4. $: Ends with anchor



Regex



Regex101 Playground



HTML5 Demo:





input:invalid {
color: red;
}

<input type=text pattern=[1-9][0-9]{5} />




[#64305] Friday, November 20, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tristab

Total Points: 735
Total Questions: 106
Total Answers: 96

Location: Grenada
Member since Sun, Dec 20, 2020
3 Years ago
tristab questions
Sat, Sep 25, 21, 00:00, 3 Years ago
Sun, Jan 31, 21, 00:00, 3 Years ago
Wed, Dec 2, 20, 00:00, 4 Years ago
Fri, Oct 23, 20, 00:00, 4 Years ago
;