Javascript UK Tax Code Validation

This JavaScript function check the format validity of a UK Tax Code specified by the supplied parameter.

The definition of a valid UK tax code has been taken from the official specification

If the parameter is in the correct format for a UK tax code, the function returns the tax code in uppercase with any spaces removed which may be used to overwrite source of the parameter. Otherwise it returns a value of false.

Try the JavaScript routine: Enter UK tax code:

In this example, the input field has an id of taxcode, and the button has an onclick="testTaxCode();" associated with it. This latter function calls the main checkTaxCode function as follows:
function testTaxCode () {
  myTaxCode = document.getElementById('taxcode').value;
  if (checkTaxCode (myTaxCode)) {
   document.getElementById('taxcode').value = checkTaxCode (myTaxCode);
    alert ("Tax Code has a valid format")
  else {
    alert ("Tax Code has an invalid format")
  alert (checkTaxCode (myTaxCode));

Download compressed JavaScript file (1,111 bytes)

Note: This routine was last updated on 22nd August 2008 to allow M1 and W1 suffixes. Many thanks to Dan Williams for his contribution.