Forum
Notifications
Clear all
Topic starter
Here is a function to check the validity of a UK (United Kingdom) business or domestic telephone number (including those associated with mobile networks) specified by the supplied parameter. The phone number will be considered as valid if it contains 10 to 11 number digits, a valid area code, has no country code, and starts with 0.
The definition of a valid telephone number has been taken from the Ofcom specification.
If the parameter has a valid format and corresponds to an appropriate telephone number, the function returns a true value. Else, It will return an error message.
This function will not accept an extension number.
Source Code
<?php
function uk_phn_vld($phn_num) {
$tmp_phn_num = str_replace (' ', '', $phn_num);
if (empty($tmp_phn_num))
return 'Telephone number not provided';
if (preg_match('/^(+)[s]*(.*)$/',$tmp_phn_num))
return 'Please do not included the country code';
$tmp_phn_num = str_replace ('-', '', $tmp_phn_num);
if (!preg_match('/^[0-9]{10,11}$/',$tmp_phn_num))
return "Wrong length, please include the area code.nYour phone numbers should be 10 or 11 digits";
if (!preg_match('/^0[0-9]{9,10}$/',$tmp_phn_num))
return 'You have entered an invalid phone number';
$phn_num_exp[0] = '/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/';
$phn_num_exp[1] = '/^02079460[0-9]{3}$/';
$phn_num_exp[2] = '/^01914980[0-9]{3}$/';
$phn_num_exp[3] = '/^02890180[0-9]{3}$/';
$phn_num_exp[4] = '/^02920180[0-9]{3}$/';
$phn_num_exp[5] = '/^01632960[0-9]{3}$/';
$phn_num_exp[6] = '/^07700900[0-9]{3}$/';
$phn_num_exp[7] = '/^08081570[0-9]{3}$/';
$phn_num_exp[8] = '/^09098790[0-9]{3}$/';
$phn_num_exp[9] = '/^03069990[0-9]{3}$/';
foreach ($phn_num_exp as $reg_exp){
if (preg_match($reg_exp,$tmp_phn_num, $mtc))
return 'You have entered an invalid phone number';
}
if (!preg_match('/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/',$tmp_phn_num))
return 'You have entered an invalid phone number';
return true;
}
?>
Usage Examples
<?php
$phn_vld = uk_phn_vld($phn_num);
if($phn_vld === true)
echo 'Valid phone number';
else
echo $phn_vld;
Posted : 03/05/2012 5:47 pm