Skip to content

Instantly share code, notes, and snippets.

@tesshsu
Last active January 10, 2018 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tesshsu/431983a5a9d9b7c2e35ed34411b01a50 to your computer and use it in GitHub Desktop.
Save tesshsu/431983a5a9d9b7c2e35ed34411b01a50 to your computer and use it in GitHub Desktop.
VAT regular Expresion
jQuery("form#adminForm, form#userForm").on("submit", function(event) {
var isTva = true;
var inputsTva = jQuery('form#adminForm #pluginistraxx_euvatchecker_field');
// define reg part, each country code has match different quantifier
// for example, GB matches the characters GB literally (case sensitive), matches a digit 0-9, Matches exactly 5 times
var regTva = /^(RO\d{2,10}|GB\d{5}|GBGD\d{3}|CHE\d{9}|CZ\d{8,10}|(ATU|DK|FI|HU|LU|MT|SI)\d{8}|IE[A-Z\d]{8}|(DE|BG|EE|EL|LT|BE0|PT)\d{9}|CY\d{8}[A-Z]|(ES|GB)[A-Z\d]{9}|(BE0|PL|SK|RU)\d{10}|(FR|IT|LV)\d{11}|(LT|SE)\d{12}|(NL|GB)[A-Z\d]{12})$;/g;
//bind each related tva input, if not pass reg, return false
jQuery.each(inputsTva, function(){
if(!regTva.test(jQuery(this).val())) {
isTva = false;
}
});
// if return false disable submit button and alert message
if (!isTva) {
event.preventDefault();
alert('PLease fill in correct TVA numbers.');
jQuery(this).prop('disabled', true);
}else {
jQuery(this).prop('disabled',false);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment