简体   繁体   English

正则表达式以验证电话号码

[英]regex to validate phone number

Help me write a regex for below conditions 帮我写以下条件的正则表达式

  1. number can start with + 数字可以以+开头
  2. number can contain - or . 数字可以包含-或。 but not () and / 但不是()和/
  3. number can start with 0 数字可以以0开头
  4. Min number in the string should be 9 digits excluding extension details and starting + 字符串中的最小数字应为9位数字(不包括扩展名和以+开头的数字)
  5. max number in the phone number field should not reach more than 14 excluding + 电话号码字段中的最大电话号码不能超过14,但+
  6. if the string contains ex/ext/x then the digit after should not have more than 5 characters (normally 4) 如果字符串包含ex / ext / x,则后面的数字不应超过5个字符(通常为4个)

this above should satisfy examples below 以上应满足以下示例

0-1234-123456
+91-1234-56789012
+91-1234-56789012 x1234
+91-1234-56789012 ex1234
+91-1234-56789012 ext12345
+91-1234-56789012x1234
+91-1234-56789012ex1234
+91-1234-56789012ext12345
91-1234-56789012
91-1234-56789012 x1234
91-1234-56789012 ex1234
91-1234-56789012 ext12345
91-1234-56789012x1234
91-1234-56789012ex1234
91-1234-56789012ext12345
91123456789012
91123456789012 x1234
91123456789012 ex1234
91123456789012 ext12345
91123456789012x1234
91123456789012ex1234
91123456789012ext12345
91.1234.56789012
91.1234.56789012 x1234
91.1234.56789012 ex1234
91.12345.6789012 ext12345
91.12345.6789012x1234
91.12345.6789012ex1234
91.12345.6789012ext12345
1-234-567-8901
1-234-567-8901 x1234
1-234-567-8901 ext1234
1 234 567-8901
1.234.567.8901
12345678901

I found few links online one of them is http://ericholmes.ca/php-phone-number-validation-revisited/ 我发现在线链接很少,其中之一是http://ericholmes.ca/php-phone-number-validation-revisited/

and on stackoverflow 并且在stackoverflow上

A comprehensive regex for phone number validation 用于电话号码验证的综合正则表达式

also

^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$

is not working for many of the above 不适用于上述多种情况

^\+?(\d[.\- ]*){9,14}(e?xt?\d{1,5})?$

Explanation; 说明;

  • ^ Asserts start of string ^断言字符串的开始
  • \\+? Matches an optional plus 匹配可选的加号
  • (\\d[.\\- ]*){9,14} between 9 and 14 single digits, possibly seperated by spaces, dots, or dashes. (\\d[.\\- ]*){9,14}介于9到14个数字之间,可能由空格,点或破折号分隔。
  • (e?xt?\\d{1,5})? Optionally ax possibly preceeded by an e or followed by a t. 可选地,ax可能以e开头或以t开头。 The letters always followed by between 1 and 5 numbers. 字母后面总是1到5个数字。
  • $ Asserts end of the string $字符串结尾

This will do it, but depending on which language you are programming in (we always need to know that with regexs, so if this doesn't work for you, reply with the language used. I've tested it in PHP5.) 可以做到这一点,但要取决于您使用的是哪种编程语言(我们始终需要使用正则表达式知道这一点,因此,如果这对您不起作用,请使用所使用的语言进行回复。我已经在PHP5中对其进行了测试。)

Your condition 5 (max 14 chars in the phone no) appears to be in error, since several of your examples contain 16 characters if they include dots or hyphens. 您的条件5(电话号码中最多14个字符)似乎是错误的,因为如果您的几个示例包含点或连字符,则它们包含16个字符。 In any case, this does not check for overall length of the whole thing because of the other length checks it does; 无论如何,这不会检查整个物件的总长度,因为它会检查另一个长度。 it would need a second regex, or, better, check the string length beforehand (eg in PHP by doing a call to strlen). 它需要第二个正则表达式,或者更好的是,事先检查字符串长度(例如,在PHP中通过调用strlen)。

You might want to allow for a space in extension numbers, eg ext 1234; 您可能需要在分机号中留一个空格,例如ext 1234;。 if so add \\s* in the appropriate place. 如果是这样,请在适当的位置添加\\ s *。

I hope this helps. 我希望这有帮助。

^\+?\d[\d-\.\s]{8,15}\s?((ext|ex|x)\d{3,5})?$

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

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