How to validate phone number using PHP

PHP code to validate phone number

There are various ways to validate a phone number in PHP. Here is an example of how to validate a phone number using regular expressions:

1 Validate phone number using PHP regular expressions

<?php
function validatePhoneNumber($phoneNumber) {
    // Regular expression to match most phone number formats
    $regex = '/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/';
    return preg_match($regex, $phoneNumber);
}

$phoneNumber = '+1 555 555 5555';
if (validatePhoneNumber($phoneNumber)) {
    echo 'Valid phone number';
} else {
    echo 'Invalid phone number';
}
?>


The regular expression '/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/' can match most phone number formats including international numbers, and numbers with or without separators.

2 Validate phone number using Google’s libphonenumber library

You can also use external libraries such as Google’s libphonenumber library, which can validate phone numbers in different countries and formats.

<?php
require_once 'path/to/libphonenumber-for-php/libphonenumber.php';

$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();

try {
    $phoneNumber = '+15555551111';
    $number = $phoneUtil->parse($phoneNumber, 'US');
    if ($phoneUtil->isValidNumber($number)) {
        echo 'Valid phone number';
    } else {
        echo 'Invalid phone number';
    }
} catch (\libphonenumber\NumberFormatException $e) {
    echo 'Invalid phone number';
}
?>


This way, you can validate phone numbers in different formats and countries.

It’s important to note that phone number validation is a complex task and the validation method you choose will depend on your specific requirements and the formats of phone numbers you expect to receive.

3 Validate using filter_var() PHP function

Here is another way to validate phone number using the filter_var() function in PHP:

<?php
function validatePhoneNumber($phoneNumber) {
    return filter_var($phoneNumber, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/")));
}

$phoneNumber = '+1 555 555 5555';
if (validatePhoneNumber($phoneNumber)) {
    echo 'Valid phone number';
} else {
    echo 'Invalid phone number';
}
?>


This way you can use the filter_var() function to check if the phone number matches the regular expression pattern and returns true if it matches, otherwise it returns false.

As you can see all of the above examples are using regular expressions to validate phone numbers, which is one way to do it, but as I mentioned before it may not be enough to cover all the possible formats and variations of phone numbers, especially when it comes to international phone numbers, so it’s a good idea to use a library like Google’s libphonenumber library that can handle phone numbers validation with more accuracy.

It’s also important to note that, it’s always a good practice to validate the phone numbers on the client side as well, to prevent malformed phone numbers from being sent to the server.

Finally, it’s a good idea to test your phone number validation function with a variety of phone numbers to ensure that it can handle different formats and variations.

Leave a Comment