简体   繁体   中英

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. 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. 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).

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. Dialing it there causes an error or a wrong number.

The area code cannot be [2-9]11 nor [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.

Exchanges also cannot begin with a 0 or 1, nor can they be [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. [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 . [If valid, this should always leave me with a 10 digit string.]

$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 .

    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.


Technically, this will validate all NANP numbers. 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.

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