Charmila
Charmila

Reputation: 455

Regular expression in ASP.net

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.

Upvotes: 0

Views: 82

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

^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.

Upvotes: 2

Spudley
Spudley

Reputation: 168655

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.

Upvotes: 1

Related Questions