4

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

ruohola
  • 21,987
  • 6
  • 62
  • 97
Vicky Kumar
  • 1,358
  • 1
  • 14
  • 26

3 Answers3

10

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.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • thanks @Wiktor this one is working if u can concept of negative lokahead it would be helpul thanks again – Vicky Kumar Sep 27 '16 at 10:19
  • I am just not sure why an empty alternative is used in the OP regex - `^([0-9]{1,10}|)$`. It may match an empty string. – Wiktor Stribiżew Sep 27 '16 at 10:19
  • it is also required if field is left empty then error should not be shown not important here – Vicky Kumar Sep 27 '16 at 10:21
  • The `[0-9]` can be simplified to just `\d`. – ruohola Nov 06 '19 at 15:17
  • @ruohola Yes, in JS, it can. However, in other regex flavors, like Python 3 `re` or .NET, `\d` will match all Unicode digits, and that is not what is expected in most cases. `\d` does not work across all regex flavors, e.g. POSIX. Thus, it is a good idea to keep to the character class solution, which is most portable. – Wiktor Stribiżew Nov 06 '19 at 15:22
2

You can check this with Number function like this.No need of regex.

var str = '10001';
console.log(Boolean(Number(str))); This returns true if string does not contain only zeros and false if string contains only zeros
ninjawarrior
  • 328
  • 1
  • 7
0

You can use regex as :^(?=.*[1-9])\d{1,10}$

Explaination :

^ assert position at start of the string

(?=.*[1-9]) Positive Lookahead - Assert that the regex below can be matched .* matches any character (except newline) Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]

[1-9] match a single character present in the list below 1-9 a single character in the range between 1 and 9

\d{1,10} match a digit [0-9] Quantifier: {1,10} Between 1 and 10 times, as many times as possible, giving back as needed [greedy]

$ assert position at end of the string

console.log("1".match(/^(?=.*[1-9])\d{1,10}$/));
console.log("1111123455".match(/^(?=.*[1-9])\d{1,10}$/));
console.log("0000".match(/^(?=.*[1-9])\d{1,10}$/));
console.log("0".match(/^(?=.*[1-9])\d{1,10}$/));
console.log("0000000000".match(/^(?=.*[1-9])\d{1,10}$/));
Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44