JavaScript EU VAT Number Validation
This function check the validity of the format of an EU VAT number specified by the supplied parameter. Note that it does not check whether the code is currently allocated - this may done by interrogating the EU database (with the exception of those uncommon numbers starting with "EU").
The definition of a valid VAT number has been taken from a variety of sources found on the web, although http://sima-pc.com/nif.php has been found to be particularly useful (but not always accurate). VAT codes include check digits - these are validated when the author has had access to a sufficiently large range of VAT numbers for that country to have allowed effective testing. Check digit validation is currently undertaken on VAT numbers for Austria, Belgium, Czech Republic (legal entities), Cyprus, Denmark, Estonia, Finland, France (old format), Germany, Greece, Hungary, Ireland, Italy, Latvia (legal entities), Lithuania (legal entities), Luxembourg, Malta, the Netherlands, Portugal, Romania, Spain, Sweden, Slovenia, and the United Kingdom. The author will be pleased to implement others if he is provided with a comprehensive test suite of valid numbers.
In this JavaScript function, the country code is mandatory apart from those belonging to the UK, in which case it is optional. A description of how to make another country's code the default is described in the embedded comments within the downloadable JavaScript code.
Embedded spaces, commas, points, and dashes are accepted, and capitalisation is ignored. If the parameter is a valid VAT number, the function returns the number with any extra characters removed and lower case alphabetic characters converted to uppercase. Otherwise it returns a value of false.
In this example, the input field has an id of vatnumber, and the button has an onclick="testVATNumber();" associated with it. This latter function calls the main checkVATNumber function as follows:
function testVATNumber () {
var myVATNumber = document.getElementById('vatnumber').value;
if (checkVATNumber (myVATNumber)) {
document.getElementById('vatnumber').value = checkVATNumber (myVATNumber)
alert ("VAT number has a valid format");
}
else alert ("VAT number has an invalid format");
}
Download compressed JavaScript file (5.41Kb)
Note: This routine was last updated on 25th September 2007 with the full validation of Spanish personal numbers. Many thanks to David Perez for his contribution.