@@ -400,6 +400,78 @@ jQueryFormUtils.validateSwedishMobileNumber = function(number) {
400
400
return ( / 0 7 [ 0 - 9 { 1 } ] / ) . test ( begin ) || ( begin === '467' && number . substr ( 3 , 1 ) === '0' ) ;
401
401
} ;
402
402
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
+
403
475
/**
404
476
* Is this a valid birth date YYYY-MM-DD
405
477
*
@@ -781,6 +853,11 @@ jQueryFormUtils.validateInput = function(el, language, config, form) {
781
853
else if ( validationRules . indexOf ( 'validate_spamcheck' ) > - 1 && ! jQueryFormUtils . simpleSpamCheck ( value , validationRules ) ) {
782
854
return language . badSecurityAnswer ;
783
855
}
856
+
857
+ // UK VAT Number check
858
+ else if ( validationRules . indexOf ( 'validate_ukvatnumber' ) > - 1 && ! jQueryFormUtils . validateUKVATNumber ( value ) ) {
859
+ return language . badUKVatAnswer ;
860
+ }
784
861
785
862
// Custom regexp validation
786
863
if ( validationRules . indexOf ( 'validate_custom' ) > - 1 && validationRules . indexOf ( 'regexp/' ) > - 1 ) {
@@ -835,7 +912,8 @@ jQueryFormUtils.LANG = {
835
912
badFloat : 'Incorrect float value' ,
836
913
badCustomVal : 'You gave an incorrect answer' ,
837
914
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'
839
917
} ;
840
918
841
919
0 commit comments