简体   繁体   English

asp.net的国际和本地电话号码正则表达式

[英]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. 我正在asp.net上工作,以防万一有人想知道。

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
  • 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 将首个匹配的电话号码范围从11增加到20

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

next step is to remove non numbers and verify length is between 8 and 12 下一步是删除非数字并验证长度在8到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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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