One bit of code I needed recently was a way to validate a user’s VAT number, but for particular countries. Please see the reasons and method for validating a user’s VAT for most of Europe.
I used the best method of using Regex to match if the user’s entered VAT matches the pattern of the particular countries format. I have seen there are ways to get a master Regex code, but then there are some loop holes in this that some can slip through. Therefore I opted to put in a Regex string for each country. You can see the source I used and more countries codes at this link https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9781449327453/ch04s21.html
To use the code snippet, just add it to you JavaScript code then add it to an ‘OnChange’ event on the select element with all the countries in. You can change the ‘CountryCode’ to be whatever coding system you us for countries as well, but I went for the widely used 2 digit country codes.
<selectOnChange="return Validate_VATNumber($(this).val(),$('vatField').val())">
<option value="GB">Great Britain<\option>
<option value="AT">Austria<\option>
<option value="BE">Belgium<\option>
</select>
function Validate_VATNumber(CountryCode, UsersVAT) {
var VATreg='';
switch (CountryCode) {
case"GB": //Great Britain
VATreg="^(GB)?([0-9]{9}([0-9]{3})?|[A-Z]{2}[0-9]{3})$";
break;
case"AT": //Austria
VATreg="^(AT)?U[0-9]{8}$";
break;
case"BE": //Belgium
VATreg="^(BE)?0[0-9]{9}$";
break;
case"BG": //Bulgaria
VATreg="^(BG)?[0-9]{9,10}$";
break;
case"HR": //Croatia
VATreg="";
break;
case"CY": //Cyprus
VATreg="^(CY)?[0-9]{8}L$";
break;
case"CZ": //Czech Republic
VATreg="^(CZ)?[0-9]{8,10}$";
break;
case"DK": //Denmark
VATreg="^(DK)?[0-9]{8}$";
break;
case"EE": //Estonia
VATreg="^(EE)?[0-9]{9}$";
break;
case"FI": //Finland
VATreg="^(FI)?[0-9]{8}$";
break;
case"FR": //France
VATreg="^(FR)?[0-9A-Z]{2}[0-9]{9}$";
break;
case"DE": //Germany
VATreg="^(DE)?[0-9]{9}$";
break;
case"GR": //Greece
VATreg="^(EL|GR)?[0-9]{9}$";
break;
case"HU": //Hungary
VATreg="^(HU)?[0-9]{8}$";
break;
case"IT": //Italy
VATreg="^(IT)?[0-9]{11}$";
break;
case"LV": //Latvia
VATreg="^(LV)?[0-9]{11}$";
break;
case"LT": //Lithuania
VATreg="^(LT)?([0-9]{9}|[0-9]{12})$";
break;
case"LU": //Luxembourg
VATreg="^(LU)?[0-9]{8}$";
break;
case"MT": //Malta
VATreg="^(MT)?[0-9]{8}$";
break;
case"NL": //Netherlands
VATreg="^(NL)?[0-9]{9}B[0-9]{2}$";
break;
case"PL": //Poland
VATreg="^(PL)?[0-9]{10}$";
break;
case"PT": //Portugal
VATreg="^(PT)?[0-9]{9}$";
break;
case"RO": //Romania
VATreg="^(RO)?[0-9]{2,10}$";
break;
case"SK": //Slovakia
VATreg="^(SK)?[0-9]{10}$";
break;
case"SI": //Slovenia
VATreg="^(SI)?[0-9]{8}$";
break;
case"ES": //Spain (exc. Canary Islands)
VATreg="^(ES)?[0-9A-Z][0-9]{7}[0-9A-Z]$";
break;
case"SE": //Sweden
VATreg="^(SE)?[0-9]{12}$";
break;
}
if (VATreg .text(UsersVAT)) {
//True
}
else {
//False
}
returnVATreg .text(UsersVAT)
};