I want to create a regular expression which will take one to ten numeric value but it should not accept if only 0's are provided
for example
1 is valid input
1111123455 is valid input
01 is valid input
010 is valid input
0000 is not valid input
0 is also not valid input
0000000000 is also not valid input
i tried regex
^([0-9]{1,10}|)$
which accepts ten numeric but how to avoid only 0's
You may use a negative lookahead:
^(?!0+$)[0-9]{1,10}$
See the regex demo
Details:
^
- start of string(?!0+$)
- no just zeros are allowed up to the end of string[0-9]{1,10}
- 1 to 10 digits$
- end of string.NOTE: To also allow empty value, use 0
as the min argument in the limiting quantifier:
^(?!0+$)[0-9]{0,10}$
^
See How Negative Lookahead Works (more here) to learn more about how (?!0+)
works in this pattern. In short: right at the start of the string, we check the whole string for just zeros. If there is a zero or more right after start of a string, the match is failed. Else, 1 (or 0) to 10 digits are matched and the result is returned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With