Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion example.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<meta charset="UTF-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.formvalidator.js"></script>

<link href="style.css" type="text/css" rel="stylesheet"/>
Expand Down Expand Up @@ -111,6 +111,11 @@ <h2>Example of all features</h2>
<strong>Valid Swedish social security number yyyymmddXXXX</strong> <em>validate_swesc</em><br/>
<input data-validation="validate_swesc"/>
</p>

<p>
<strong>UK VAT Number</strong> <em>validate_ukvatnumber</em><br/>
<input name="4" data-validation="validate_ukvatnumber"/>
</p>

<p>
<strong>Custom validation</strong>
Expand Down
84 changes: 82 additions & 2 deletions jquery.formvalidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,78 @@ jQueryFormUtils.validateSwedishMobileNumber = function(number) {
return (/07[0-9{1}]/).test(begin) || (begin === '467' && number.substr(3, 1) === '0');
};

/**
* Validate that string is a UK VAT Number
* TODO: Extra Checking for other VAT Numbers (Other countries and UK Government/Health Authorities)
* Code Adapted from http://www.codingforums.com/showthread.php?t=211967
*
* @param {Number}
* @return {Boolean}
*/
jQueryFormUtils.validateUKVATNumber = function(number) {
number = number.replace(/[^0-9]/g, '');

//Check Length
if(number.length < 9) {
return false;
}

var valid = false;

var VATsplit = [];
VATsplit = number.split("");

var checkDigits = Number(VATsplit[7] + VATsplit[8]); // two final digits as a number

var firstDigit = VATsplit[0];
var secondDigit = VATsplit[1];
if ((firstDigit == 0) && (secondDigit >0)) {
return false;
}

var total = 0;
for (var i=0; i<7; i++) { // first 7 digits
total += VATsplit[i] * (8-i); // sum weighted cumulative total
}

var c = 0;
var i = 0;

for (var m = 8; m>=2; m--) {
c += VATsplit[i] * m;
i++;
}

// Traditional Algorithm for VAT numbers issued before 2010

while (total > 0) {
total -= 97; // deduct 97 repeatedly until total is negative
}
total = Math.abs(total); // make positive

if (checkDigits == total) {
valid = true;
}

// 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

if (!valid) {
total = total%97 // modulus 97

if (total >= 55) {
total = total - 55
} else {
total = total + 42
}

if (total == checkDigits) {
valid = true;
}
}

return valid;
};

/**
* Is this a valid birth date YYYY-MM-DD
*
Expand Down Expand Up @@ -467,7 +539,9 @@ jQueryFormUtils.parseDate = function(val, dateFormat) {
var day = findDateUnit('d', formatParts, matches);
var year = findDateUnit('y', formatParts, matches);

if (month === 2 && day > 28 || month > 12 || month === 0) {
if ((month === 2 && day > 28 && (year % 4 !== 0 || year % 100 === 0 && year % 400 !== 0))
|| (month === 2 && day > 29 && (year % 4 === 0 || year % 100 !== 0 && year % 400 === 0))
|| month > 12 || month === 0) {
return false;
}
if ((this.isShortMonth(month) && day > 30) || (!this.isShortMonth(month) && day > 31) || day === 0) {
Expand Down Expand Up @@ -779,6 +853,11 @@ jQueryFormUtils.validateInput = function(el, language, config, form) {
else if (validationRules.indexOf('validate_spamcheck') > -1 && !jQueryFormUtils.simpleSpamCheck(value, validationRules)) {
return language.badSecurityAnswer;
}

// UK VAT Number check
else if (validationRules.indexOf('validate_ukvatnumber') > -1 && !jQueryFormUtils.validateUKVATNumber(value)) {
return language.badUKVatAnswer;
}

// Custom regexp validation
if (validationRules.indexOf('validate_custom') > -1 && validationRules.indexOf('regexp/') > -1) {
Expand Down Expand Up @@ -833,7 +912,8 @@ jQueryFormUtils.LANG = {
badFloat : 'Incorrect float value',
badCustomVal : 'You gave an incorrect answer',
badInt : 'Incorrect integer value',
badSecurityNumber : 'Your social security number was incorrect'
badSecurityNumber : 'Your social security number was incorrect',
badUKVatAnswer : 'Incorrect UK VAT Number'
};


Expand Down
Loading