php - looking to validate US phone number w/ area code

145

I'm working on a function to validate a US phone number submitted by a user, which can be submitted in any of the popular number formats people usually use. My code so far is as follows:

$number = '123-456-7890';

function validate_telephone_number($number) {
    $formats = array(
        '###-###-####',
        '(###)###-###',
        '(###) ###-###',
        '##########'
    );

    $number = trim(preg_replace('[0-9]', '#', $number));

    if (in_array($number, $formats)) {
        return true;
    } else {
        return false;
    }
}

First off, this code does not seem to be working, and returns false on all submitted numbers. I can't seem to find my error.

Secondly, I'm looking for an easy way to only allow phone numbers from an array of specific allowed area codes. Any ideas?

397

Answer

Solution:

For your first question:

preg_replace('/[0-9]/', '#', $number)

or'/\d/'


For the second question this may help you:

$areaCode = substr(preg_replace('/[^\d]/', '', $number),0 , 3);

This will give you the first 3 digits in the number by discarding all other characters.

I'm not familiar with the US area codes format so I cannot help you more with this one.


Bonus:

if (in_array($number, $formats)) {
    return true;
} else {
    return false;
}

is equivalent to

return in_array($number, $formats);

As a matter of fact any statement of the form

if(<expression>){
    return true;
}
else{
    return false;
}

can be written asreturn (bool) <expr>;, but in this casein_array will always return a Boolean so(bool) is not needed.

67

Answer

Solution:

Your code does not check for well formatted but invalid numbers - for example, no area code starts with 0 or 1 in the US, so this could be checked. Also, your formats do not allow for country code inclusion - +15551234567 would be rejected, for example.

If you don't care about the formatting and just want to validate if the digits in the input amount to a valid US phone number, you could use something like this:

$clean_number = preg_replace("/[^0-9]/", '', $number);
$valid = preg_match("/^(\+?1)?[2-9][0-9]{9}$/", $clean_number);

Of course, this will also accept "foo 5555555555 bar" as a valid number - if you want to disallow that, make the preg_replace more restrictive (e.g, remove only brackets, spaces and dashes).

345

Answer

Solution:

If you prefer to do this without maintaining a lot of code, you an check out this API that validates a US number and provides several formats for the number https://www.mashape.com/parsify/format

475

Answer

Solution:

Look here for a code project that has a function for validating phone numbers.

People are also looking for solutions to the problem: php - How to run an action of controller file from .ctp file?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.