Skip to content

Commit 0e3fb82

Browse files
author
Matt Clements
committed
UK VAT Number Validation
UK VAT Number Validation added
1 parent 4f0491e commit 0e3fb82

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

example.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ <h2>Example of all features</h2>
111111
<strong>Valid Swedish social security number yyyymmddXXXX</strong> <em>validate_swesc</em><br/>
112112
<input data-validation="validate_swesc"/>
113113
</p>
114+
115+
<p>
116+
<strong>UK VAT Number</strong> <em>validate_ukvatnumber</em><br/>
117+
<input name="4" data-validation="validate_ukvatnumber"/>
118+
</p>
114119

115120
<p>
116121
<strong>Custom validation</strong>

jquery.formvalidator.js

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,78 @@ jQueryFormUtils.validateSwedishMobileNumber = function(number) {
400400
return (/07[0-9{1}]/).test(begin) || (begin === '467' && number.substr(3, 1) === '0');
401401
};
402402

403+
/**
404+
* Validate that string is a UK VAT Number
405+
* TODO: Extra Checking for other VAT Numbers (Other countries and UK Government/Health Authorities)
406+
* Code Adapted from http://www.codingforums.com/showthread.php?t=211967
407+
*
408+
* @param {Number}
409+
* @return {Boolean}
410+
*/
411+
jQueryFormUtils.validateUKVATNumber = function(number) {
412+
number = number.replace(/[^0-9]/g, '');
413+
414+
//Check Length
415+
if(number.length < 9) {
416+
return false;
417+
}
418+
419+
var valid = false;
420+
421+
var VATsplit = [];
422+
VATsplit = number.split("");
423+
424+
var checkDigits = Number(VATsplit[7] + VATsplit[8]); // two final digits as a number
425+
426+
var firstDigit = VATsplit[0];
427+
var secondDigit = VATsplit[1];
428+
if ((firstDigit == 0) && (secondDigit >0)) {
429+
return false;
430+
}
431+
432+
var total = 0;
433+
for (var i=0; i<7; i++) { // first 7 digits
434+
total += VATsplit[i] * (8-i); // sum weighted cumulative total
435+
}
436+
437+
var c = 0;
438+
var i = 0;
439+
440+
for (var m = 8; m>=2; m--) {
441+
c += VATsplit[i] * m;
442+
i++;
443+
}
444+
445+
// Traditional Algorithm for VAT numbers issued before 2010
446+
447+
while (total > 0) {
448+
total -= 97; // deduct 97 repeatedly until total is negative
449+
}
450+
total = Math.abs(total); // make positive
451+
452+
if (checkDigits == total) {
453+
valid = true;
454+
}
455+
456+
// If not valid try the new method (introduced November 2009) by subtracting 55 from the mod 97 check digit if we can - else add 42
457+
458+
if (!valid) {
459+
total = total%97 // modulus 97
460+
461+
if (total >= 55) {
462+
total = total - 55
463+
} else {
464+
total = total + 42
465+
}
466+
467+
if (total == checkDigits) {
468+
valid = true;
469+
}
470+
}
471+
472+
return valid;
473+
};
474+
403475
/**
404476
* Is this a valid birth date YYYY-MM-DD
405477
*
@@ -781,6 +853,11 @@ jQueryFormUtils.validateInput = function(el, language, config, form) {
781853
else if (validationRules.indexOf('validate_spamcheck') > -1 && !jQueryFormUtils.simpleSpamCheck(value, validationRules)) {
782854
return language.badSecurityAnswer;
783855
}
856+
857+
// UK VAT Number check
858+
else if (validationRules.indexOf('validate_ukvatnumber') > -1 && !jQueryFormUtils.validateUKVATNumber(value)) {
859+
return language.badUKVatAnswer;
860+
}
784861

785862
// Custom regexp validation
786863
if (validationRules.indexOf('validate_custom') > -1 && validationRules.indexOf('regexp/') > -1) {
@@ -835,7 +912,8 @@ jQueryFormUtils.LANG = {
835912
badFloat : 'Incorrect float value',
836913
badCustomVal : 'You gave an incorrect answer',
837914
badInt : 'Incorrect integer value',
838-
badSecurityNumber : 'Your social security number was incorrect'
915+
badSecurityNumber : 'Your social security number was incorrect',
916+
badUKVatAnswer : 'Incorrect UK VAT Number'
839917
};
840918

841919

0 commit comments

Comments
 (0)