简体   繁体   中英

Regular Expression for asp.net for International & local phone numbers

I am using following regular expression to cover international phone numbers & some local phone number which can be in this format only

International Phone number

+123 456789123
+123456789123

+12 3456789123
+123456789123

Local Phone number format (Mobile no. followed by landlines numbers)

1234567890      
123 4567890

123123456
12 3123456

Regular expression which i am using

^[\+]{0,1}[1-9]{1}[0-9]{7,11}$

This regular expression works well with international numbers only irrespective of prefix + is added or not but doesn't allow any while space character.

I want it to support above formats for as show in example and should also support all international phone numbers

I am working on asp.net just in case if some one wants to know.

UPDATE:

I finally end up using following Regex which also handles extension

^([\+]?[0-9]{1,3}[\s.-][0-9]{1,12})([\s.-]?[0-9]{1,4}?)$

Hi some comment about you're regex

[\+]{0,1} could be \+?  // easier to read, + as optional
[1-9]{1} could be writen as [1-9]
[0-9]{7,11} should be [0-9\s.-]{7,11}  // phone numbers van contain - or .

You totaly regex would be

^\+?[1-9][0-9\s.-]{7,11}$

phone numbers could be writen as

  • 070-3233123
  • 070.3233123

SECOND ATTEMPT

You could break youre problems in 2 steps:

First match possibele phone number by increasing range from 11 to 20

^\\+?[1-9][0-9\\s.-]{7,20}$

next step is to remove non numbers and verify length is between 8 and 12

string phone = "070.3233123";

string onlyNumbers= new String(phone.ToCharArray().Where(c => Char.IsDigit(c)).ToArray());

if (onlyNumbers.length > 8 &&  onlyNumbers.length < 12)
{
    // we got a winner
}

尝试这个:

^[\+]?[1-9]{1,3}\s?[0-9]{6,11}$

Below pattern will validate the following format

^\s*\+?[0-9]\d?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$

Results

+1 (281) 388 0388
+65 281 388-0388
+91 281 388 0388
+65 2813880388
+652813880388
+65(281)3880388
+65281388-0388
+1281388-0388

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