Browse Source

#179: Validate Lithuanian VAT number

phuoc 11 years ago
parent
commit
e06551b7f3
3 changed files with 74 additions and 22 deletions
  1. 37 20
      dist/js/bootstrapValidator.js
  2. 2 2
      dist/js/bootstrapValidator.min.js
  3. 35 0
      src/js/validator/vat.js

+ 37 - 20
dist/js/bootstrapValidator.js

@@ -792,7 +792,7 @@
     // Helper methods, which can be used in validator class
     $.fn.bootstrapValidator.helpers = {
         /**
-         * Implement Luhn validation algorithm
+         * Implement Luhn validation algorithm ((http://en.wikipedia.org/wiki/Luhn))
          * Credit to https://gist.github.com/ShirtlessKirk/2134376
          *
          * @param {String} value
@@ -963,25 +963,7 @@
             }
             value = value.replace(/\D/g, '');
 
-            // Validate the check sum
-            // The Luhn Algorithm
-            // http://en.wikipedia.org/wiki/Luhn
-            var check = 0, digit = 0, even = false, length = value.length;
-
-            for (var n = length - 1; n >= 0; n--) {
-                digit = parseInt(value.charAt(n), 10);
-
-                if (even) {
-                    if ((digit *= 2) > 9) {
-                        digit -= 9;
-                    }
-                }
-
-                check += digit;
-                even = !even;
-            }
-
-            if ((check % 10) != 0) {
+            if (!$.fn.bootstrapValidator.helpers.luhn(value)) {
                 return false;
             }
 
@@ -3165,6 +3147,41 @@
         },
 
         /**
+         * Validate Lithuanian VAT number
+         * It can be:
+         * - 9 digits, for legal entities
+         * - 12 digits, for temporarily registered taxpayers
+         *
+         * Examples:
+         * - Valid: LT119511515, LT100001919017, LT100004801610
+         * - Invalid: LT100001919018
+         *
+         * @param {String} value VAT number
+         * @returns {Boolean}
+         */
+        _lt: function(value) {
+            if (!/^LT([0-9]{7}1[0-9]{1}|[0-9]{10}1[0-9]{1})$/.test(value)) {
+                return false;
+            }
+
+            value = value.substr(2);
+            var length = value.length,
+                sum    = 0;
+            for (var i = 0; i < length - 1; i++) {
+                sum += parseInt(value.charAt(i)) * (1 + i % 9);
+            }
+            var check = sum % 11;
+            if (check == 10) {
+                sum = 0;
+                for (var i = 0; i < length - 1; i++) {
+                    sum += parseInt(value.charAt(i)) * (1 + (i + 2) % 9);
+                }
+            }
+            check = check % 11 % 10;
+            return (check == value.charAt(length - 1));
+        },
+
+        /**
          * Validate Maltese VAT number
          * Examples:
          * - Valid: MT11679112

File diff suppressed because it is too large
+ 2 - 2
dist/js/bootstrapValidator.min.js


+ 35 - 0
src/js/validator/vat.js

@@ -767,6 +767,41 @@
         },
 
         /**
+         * Validate Lithuanian VAT number
+         * It can be:
+         * - 9 digits, for legal entities
+         * - 12 digits, for temporarily registered taxpayers
+         *
+         * Examples:
+         * - Valid: LT119511515, LT100001919017, LT100004801610
+         * - Invalid: LT100001919018
+         *
+         * @param {String} value VAT number
+         * @returns {Boolean}
+         */
+        _lt: function(value) {
+            if (!/^LT([0-9]{7}1[0-9]{1}|[0-9]{10}1[0-9]{1})$/.test(value)) {
+                return false;
+            }
+
+            value = value.substr(2);
+            var length = value.length,
+                sum    = 0;
+            for (var i = 0; i < length - 1; i++) {
+                sum += parseInt(value.charAt(i)) * (1 + i % 9);
+            }
+            var check = sum % 11;
+            if (check == 10) {
+                sum = 0;
+                for (var i = 0; i < length - 1; i++) {
+                    sum += parseInt(value.charAt(i)) * (1 + (i + 2) % 9);
+                }
+            }
+            check = check % 11 % 10;
+            return (check == value.charAt(length - 1));
+        },
+
+        /**
          * Validate Maltese VAT number
          * Examples:
          * - Valid: MT11679112