1110
1110

Reputation: 6829

How to make regex that validate country code in phone number

I am creating asp.net regex validator that should validate mobile number with country code.
Number can be like: +41 44 221 21 20 or 0041 44 221 21 20 or same without spaces.
I have tried something but it doesn't work:

\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)
\W*\d\W*\d\W*\d\W*\d\W*\d\W*\d\W*\d\W*\d\W*(\d{1,2})$

How to make regex for this validation?

Upvotes: 0

Views: 3914

Answers (1)

OGHaza
OGHaza

Reputation: 4795

Assuming that your list of country codes is correct, the following should work:

^(\+|00)(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)(\s?\d){9}$

Matches can start with + or 00, followed by a country code, followed by a sequence of 9 numbers which can be divided by spaces.

Note: If you are trying to match these numbers inside a longer string, remove the ^ and $ from the ends of the regex.

Working on RegexPal

Upvotes: 2

Related Questions