Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
165
rated 0 times [  167] [ 2]  / answers: 1 / hits: 32462  / 11 Years ago, thu, june 6, 2013, 12:00:00

I need to validate some inputs using a regex.



Here are some sample use cases and expected results.



0001     - Matched
001 - Matched
01 - Matched
00.12312 - Matched
000.1232 - Matched
1 - Not matched
20 - Not matched
0.1 - Not matched
0.123123 - Not matched


What would a regex like this look like? If first char is 0 and second char is numerical[0-9] then it is invalid.



I've tried this but it doesn't work.



[0][0-9]

More From » regex

 Answers
15

Try this regex:



^0[0-9].*$


It will match anything starting with 0 followed by a digit.



It will match what you call invalid.



The test code shall make it clearer:



var regExp = /^0[0-9].*$/
console.log(regExp.test(0001)); // true
console.log(regExp.test(001)); // true
console.log(regExp.test(01)); // true
console.log(regExp.test(00.12312)); // true
console.log(regExp.test(000.1232)); // true
console.log(regExp.test(1)); // false
console.log(regExp.test(20)); // false
console.log(regExp.test(0.1)); // false
console.log(regExp.test(0.123123)); // false

[#77771] Wednesday, June 5, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ravenl

Total Points: 338
Total Questions: 107
Total Answers: 112

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
ravenl questions
Thu, Feb 18, 21, 00:00, 3 Years ago
Tue, Jan 12, 21, 00:00, 3 Years ago
Tue, Mar 17, 20, 00:00, 4 Years ago
;