简体   繁体   English

仅验证美国电话号码

[英]Validate U.S. Phone Numbers Only

I am presently building a website, in PHP, that users will be able to interact with via SMS messaging. 我目前正在建立一个PHP网站,用户可以通过SMS消息进行交互。 Consequently, I need to store their phone numbers in my database. 因此,我需要将他们的电话号码存储在数据库中。 Since I am only allowing SMS interactions in the US right now, I need to check that the submitted phone numbers conform to the US format. 由于我现在仅允许在美国进行SMS交互,因此我需要检查提交的电话号码是否符合美国格式。 Generally, this means a few things: 1 must be the first number, the area code must not begin with 0 or 1 , and the string without the leading 1 must be 10 digits (3 digit area code plus 7 digit number). 通常,这意味着几件事: 1必须是第一个数字,区号不能以01开头,并且不带前导1的字符串必须是10位数字(3位区号加7位数字)。

Despite all of these requirements, I want users to feel free to enter their number in whatever format they prefer without causing an error. 尽管有所有这些要求,但我希望用户可以随意选择自己喜欢的格式输入他们的电话号码,而不会引起错误。

Ignoring non-digits is the first step. 忽略非数字是第一步。 The leading 1 is not required by all telephone companies, particularly near New Jersey. 并非所有电话公司都要求使用前导1,尤其是在新泽西州附近。 Dialing it there causes an error or a wrong number. 在此拨号会导致错误或错误的号码。

The area code cannot be [2-9]11 nor [2-9]9[0-9]. 区号不能为[2-9] 11或[2-9] 9 [0-9]。 Area codes with a 9 as the center digit are reserved for an as-yet-undecided scheme to address area code exhaustion. 以9为中心的区号保留给尚未确定的方案,以解决区号耗尽的问题。

Exchanges also cannot begin with a 0 or 1, nor can they be [2-9]11. 交换也不能以0或1开头,也不能为[2-9] 11。


These restrictions are expressed with this code: 这些限制用以下代码表示:

$mobile = preg_replace ('/\D/', '', $trimmed['mobile']);

if ($mobile[0] == '1') $mobile = substr ($mobile, 1);  // remove prefix

$invalid = strlen ($mobile) != 10  ||
           preg_match ('/^1/',      $mobile) ||  // ac start with 1
           preg_match ('/^.11/',    $mobile) ||  // telco services
           preg_match ('/^...1/',   $mobile) ||  // exchange start with 1
           preg_match ('/^....11/', $mobile) ||  // exchange services
           preg_match ('/^.9/',     $mobile);    // ac center digit 9

After learning what I could from searching and scouring, I could not find a US only method. 在从搜索和精练中学到了什么之后,我找不到美国唯一的方法。 So, here is what I devised: 所以,这是我设计的:

First, to allow for any input format, use preg_replace to remove all non-digit characters. 首先,要允许任何输入格式,请使用preg_replace删除所有非数字字符。 [Users can use whatever hyphens or slashes they want, but they are of no use to me.] [用户可以使用所需的任何连字符或斜杠,但对我来说毫无用处。]

$mobile = preg_replace('/\D/', '', $trimmed['mobile']);

With just a pure string of digits to work with, eliminate any leading 1 s — this takes care of any instance where the user included the leading 1 or where they started the area code with 1 . 仅使用纯数字串即可,消除任何前导1 ,这可以照顾到用户包括前导1或以1开头区号的任何情况。 [If valid, this should always leave me with a 10 digit string.] [如果有效,这应该总是给我留下10位数字的字符串。]

$mobiletrim = ltrim($mobile, '1');

Next, regex checks that the trimmed phone number is precisely 10 digits long and does not begin with either a 0 or a 1 . 接下来,正则表达式检查所修剪的电话号码的长度是否恰好是10位数字,并且开头不是0还是1

    if (preg_match ('/^[2-9]\d{9}$/', $mobiletrim) || empty($trimmed['mobile'])) {
        $mp = TRUE;
        if (!empty($trimmed['mobile'])) { $mobiletrim = "1" . $mobiletrim; }
    } else {
        $profile_errors[] = "Please enter a valid U.S. phone number.";
    }
if ($mp) { //Store Phone Number in db }

The final step, which you can see above, was to add the leading 1 before storing the valid number in the db, provided that the user did not submit an empty phone field, which they would do if they didn't want their number stored. 您可以在上面看到的最后一步是,如果用户未提交空电话字段,则在将有效号码存储在数据库中之前添加前导1 ,如果他们不希望存储其号码,则可以这样做。 。


Technically, this will validate all NANP numbers. 从技术上讲,这将验证所有NANP编号。 The only way to narrow it down from NANP to US exclusively would be to verify that a US area code was used by parsing through a list of current US area codes. 唯一将其从NANP缩小到美国范围的唯一方法是通过解析当前美国区号列表来验证是否使用了美国区号。

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

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