How to Validate Phone Numbers in PHP

Cloudmersive
2 min readJun 4, 2024

--

Rigorously validating phone numbers (or any other contact information) makes our lives much, much easier in the long run.

If we’re accepting Phone Number text inputs in a web form, we’ll need to make sure we validate that information properly in our PHP form handler.

We can easily accomplish that with a free API, and we can use the complementary, ready-to-run PHP code examples further down the page to structure our API call.

This API will process phone numbers in conjunction with country codes to determine their validity, and it’ll return a few additional lines of structured information in our response object. That includes a version of the original input number structured according to the national formatting conventions of the given country code.

Let’s take the following input, for example (note the country code MUST be defined in our request):

{
"PhoneNumber": "800-463-3339",
"DefaultCountryCode": "US"
}

For this input, we’ll receive a response like the following example:

{
"IsValid": true,
"Successful": true,
"PhoneNumberType": "TollFree",
"E164Format": "+18004633339",
"InternationalFormat": "+1 800-463-3339",
"NationalFormat": "(800) 463-3339",
"CountryCode": "US",
"CountryName": "United States"
}

We can use this response to easily send error messages back to the user for invalid phone numbers, and we can commit the reformatted valid number entries to our contact database.

We can structure our API call in two easy steps.

First, let’s run the following command to install the SDK via Composer:

composer require cloudmersive/cloudmersive_validate_api_client

Next, let’s quickly shift our attention to API authorization. We’ll need a free Cloudmersive API key to authorize our API calls; this will allow us to make up to 800 API calls per month with zero commitments.

With our API key ready, we can use the following code examples to call the validation function. We can replace the ‘YOUR_API_KEY’ placeholder text with our own free API key:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\PhoneNumberApi(


new GuzzleHttp\Client(),
$config
);
$value = new \Swagger\Client\Model\PhoneNumberValidateRequest(); // \Swagger\Client\Model\PhoneNumberValidateRequest | Phone number to validate in a PhoneNumberValidateRequest object. Try a phone number such as \"1.800.463.3339\", and either leave DefaultCountryCode blank or use \"US\".

try {
$result = $apiInstance->phoneNumberSyntaxOnly($value);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling PhoneNumberApi->phoneNumberSyntaxOnly: ', $e->getMessage(), PHP_EOL;
}
?>

That’s all there is to it! Now we can easily validate phone number inputs with just a few lines of code.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.