Skip to main content

Phone validation

phoneNumberValidation() checks whether a phone number is valid and returns it in national and international format, with its country and network.

phone.phpphp
use IdealPostcodes\IdealPostcodes;
use IdealPostcodes\Types\PhoneNumber;

$client = IdealPostcodes::client('ak_test');
$phone = $client->phoneNumberValidation(['query' => '+44 20 7112 8019'])->result;

if ($phone instanceof PhoneNumber) {
echo $phone->internationalFormat, ' ', $phone->country, ' ', $phone->originalCarrier?->name, PHP_EOL;
}

Include the country code. A UK number can start +44, 44 or 0044. Your key needs phone validation enabled.

What does it return?​

A valid number returns a PhoneNumber with:

PropertyExample
nationalFormat020 7112 8019
internationalFormat+44 20 7112 8019
isoCountry and isoCountry2GBR and GB
countryUnited Kingdom
originalCarrierA Carrier for the network the number was first assigned to, with name and networkType such as mobile or landline

An invalid number returns an InvalidPhoneNumber with valid false and every other property null. It is not an error, so check the class before reading the formats.

Can I find the current network?​

Add 'current_carrier' => 'true' alongside query to fill currentCarrier as well. A number can move to another network, so originalCarrier alone can be out of date. The extra check can be slow, depending on the network, so allow for it in your request timeout.

What does it cost?​

One lookup per request, including numbers that are invalid or fail to parse.

See validate for the parameters and the phone validation guide for how the checks work.