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.

  1. A valid telephone number has been provided
  2. No number has been provided
  3. Telephone number does not contain the required 10 or 11 digits (the specification indicates 11 digits, but the author knows of some 10 digit numbers)
  4. Valid full UK telephone numbers must start with a 0
  5. The number contains an inappropriate or invalid code

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.

Try the Routine: Enter telephone number:

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.

See here for the routines' change log.

Download compressed JavaScript, VBScript & PHP files (4.98 Kb)