简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM