I want to validate a 5 digit number which should not be 00000. All numbers except 00000 are allowed.
examples : 01201 , 00001, 21436 , 45645 are valid numbers and 1,
Using negative positive lookahead:
/^(?!0{5})\d{5}$/
/^(?!0{5})\d{5}$/.test('01201')
// => true
/^(?!0{5})\d{5}$/.test('00001')
// => true
/^(?!0{5})\d{5}$/.test('21436')
// => true
/^(?!0{5})\d{5}$/.test('1')
// => false
/^(?!0{5})\d{5}$/.test('12')
// => false
/^(?!0{5})\d{5}$/.test('00000')
// => false
No need for negative lookahead
Live Demo
function isvalid5(str) {
return str != "00000" && /^\d{5}$/.test(str);
}
for regex engines that do not support lookahead:
^(0000[1-9]|000[1-9][0-9]|00[1-9][0-9]{2}|0[1-9][0-9]{3}|[1-9][0-9]{4})$
You can use a negative look ahead:
^(?!0{5})\d{5}$
The negative loo ahead (?!...)
will fail the whole regex if what's inside it matches, \d
is a shortcut for [0-9]
.