UK Telephone Number Validation - JavaScript, VBScript, & PHP
These functions check the validity of a UK business or domestic telephone number (including those associated with mobile networks) specified by the supplied parameter. All inappropriate telephone numbers (e.g. premium lines, adult services, radio-paging services, numbers allocated for drama purposes etc.) are disallowed. The country code is also not allowed, but hyphens and spaces are accepted. JavaScript, VBScript and PHP versions are available.
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 the number correctly formatted - i.e. without hyphens and spaces. It also returns an appropriate error description as follows.
These routines will not accept an extension number. If you need one, then it is suggested that either you modify your chosen routine as required, or include a separate field.
In this example, the input field has an id of telnumber, and the button has an onClick="testTelNumber();" associated with it. This latter function calls the main checkUKTelephone function as follows:
function testTelNumber () {
var myTelNo = document.getElementById('telnumber').value;
// If invalid number, report back error
if (!checkUKTelephone (myTelNo)) {
alert (telNumberErrors[telNumberErrorNo]);
}
// Otherwise redisplay telephone number on form in corrected format
else {
document.getElementById('telnumber').value = checkUKTelephone (myTelNo);
alert ("Telephone number appears to be valid");
}
}
Note that the calling sequences differ significantly between the JavaScript version and the other two versions. The JavaScript uses global variables to return the error text, and the function to return the reformated telephone number, whereas the VBScript and PHP versions use parameters to return information. Full details may be found in the source code.
Download compressed JavaScript, VBScript & PHP files (4.98 Kb)
Note: This routine was updated on the 4th August 2006 to include 03 numbers which are being brought in by Ofcom in early 2007, and updated on the 9th January 2007 to include 07624 (Isle of Man mobile) numbers. It was further updated on the 7th November 2007 to improve support for mobile numbers, and on 14th April 2008 to exclude numbers allocated for drama purposes.