简体   繁体   English

使用国家代码检查的电话号码的正则表达式(RegEx)

[英]Regular Expression (RegEx) For Phone Numbers With Country Code Check

I need a PHP RegEx through which I can validate a phone number format using the following criteria: 我需要一个PHP RegEx,通过它我可以使用以下标准验证电话号码格式:

  • Should not include anything except for numbers; 除数字外不应包含任何内容;
  • Should not start with a zero as I need to have the country code prefixed; 不应该从零开始,因为我需要以国家代码为前缀;
  • The list of allowed country codes should be there in RegEx; RegEx中应包含允许的国家/地区代码列表;
  • The digit immediately after the country code should not be a zero; 国家代码后面的数字不应为零;
  • The maximum length of the number should not exceed 13 digits. 该号码的最大长度不应超过13位。

I have tried to search on Stack Overflow before posting this question but couldn't find the exact solution. 在发布此问题之前,我曾尝试搜索Stack Overflow但无法找到确切的解决方案。 Any help would be highly appreciated. 任何帮助将受到高度赞赏。

Edit: I just want the user to enter the phone number in a valid format as currently my clients do some silly formatting mistakes while writing it. 编辑:我只是希望用户以有效的格式输入电话号码,因为目前我的客户在编写时会犯一些愚蠢的格式错误。 I am not worried about it being actually valid (call-able) as the user will take care of that himself. 我不担心它实际上是有效的(可调用),因为用户将自己处理。

Regards 问候

I wouldn't burn my fingers on this. 我不会把手指烧在这上面。

Just by looking at a phone number you can not judge whether it is valid or not, and even if it 'looks' valid it might not be the phone number you're looking for (ie. someone else's phone number). 只是通过查看电话号码,您无法判断它是否有效,即使它“看起来”有效,也可能不是您正在寻找的电话号码(即其他人的电话号码)。

You're trying to solve the problem at the wrong level. 你试图在错误的水平解决问题。 The only way to validate a phone number is to actually CALL it ;) 验证电话号码的唯一方法是实际调用它;)

A regex that suffices your criteria: 一个满足您标准的正则表达式:

/^(1|20|21|..etc)[1-9][0-9]+$/

list of country codes (seperated by | ) followed by a digit that is not 0, followed by any digit any number of times. 国家/地区代码列表(由|分隔)后跟一个非0的数字,后跟任意数字的任意数字。

You can can't do the 13-length check and the country check in one regex because the length of the country codes vary (to my knowledge at least). 你不能在一个正则表达式中进行13长度检查和国家检查,因为国家代码的长度不同(据我所知)。 You can do the 13-length check by a generic regex like: 您可以使用通用正则表达式执行13长度检查,例如:

/^.{,13}$/

Matching anything up to 13 characters long 匹配长达13个字符的任何内容

The digit immediately after the country code should not be a zero; 国家代码后面的数字不应为零;

That system will fail for Italy. 那个系统将失败意大利。 The leading 0 for Italian phone numbers is dialled from abroad. 意大利电话号码0领先从国外拨打。

They might be some other countries where that also happens, but Italy is the most well-known. 他们可能是其他一些也会发生这种情况的国家,但意大利是最知名的国家。

The maximum length of the number should not exceed 13 digits. 该号码的最大长度不应超过13位。

For shorter numbers but with an extension added, that rule will be broken in multiple countries. 对于较短的数字但添加了扩展名,该规则将在多个国家/地区中断。

With 3 digit country code, 3 digit area code and 8 digit subscriber number, you're a digit short. 使用3位国家代码,3位数区号和8位用户号码,您只需数字。

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

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