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.
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 (878 bytes)
Note: This routine was last updated on 13th March 2008 to correct a K code error. Many thanks to Adam Deacon for his contribution.