0

I wrote regular expression for phone no as ^[0]\d{9,10} (phone no should start with 0). This works fine.

But I want to omit the option repeating 0's. i.e 0000000000

How can I add this bit to it.

0

2 Answers 2

2
^0(?!0*$)\d{9,10}$

might be what you want.

The first number is a 0.

The (?!0*$) negative lookahead ensures that the rest of the string is not all zeroes.

And finally \d{9,10} matches any 9 or 10 digits.

Sign up to request clarification or add additional context in comments.

Comments

1

You could specify [1-9] as the second digit instead of \d, like so:

^[0][1-9]\d{8,9}$

(I presume the rest of the digits could still be zeros)

I do note that your phone number format is fairly limited. For example, it doesn't allow for international numbers (starting with a plus sign), nor for any common formatting characters such as brackets spaces or hyphens. It also assumes that all phone numbers will be 10 or 11 digits long, which is (mostly) true in the UK and probably other countries, but may not always be the case.

Depending on the requirements of your system, you may want to adjust to take some of those points into account.

2 Comments

Thanks, But this would avoid 0 as the 2nd character
@Charmila - is it valid to have zero for the second character? (I guess it is if you're allowing international numbers to begin 00 rather than with a plus, but then you'd need to allow for longer numbers if you're allowing international numbers)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.