浏览代码

Implement modulus 11, 10 (ISO 7064) algorithm

phuoc 11 年之前
父节点
当前提交
fb6b3104ec
共有 5 个文件被更改,包括 40 次插入52 次删除
  1. 2 0
      CHANGELOG.md
  2. 18 25
      dist/js/bootstrapValidator.js
  3. 2 2
      dist/js/bootstrapValidator.min.js
  4. 16 1
      src/js/bootstrapValidator.js
  5. 2 24
      src/js/validator/vat.js

+ 2 - 0
CHANGELOG.md

@@ -2,6 +2,7 @@
 
 ## v0.4.4 (not released yet)
 
+* Add ```$.fn.bootstrapValidator.helpers.mod_11_10``` method that implements modulus 11, 10 (ISO 7064) algorithm. The helper is then reused in validating [German and Croatian VAT](http://bootstrapvalidator.com/validators/vat/) numbers
 * [#213](https://github.com/nghuuphuoc/bootstrapvalidator/issues/213): Add EAN (International Article Number) validator
 * [#215](https://github.com/nghuuphuoc/bootstrapvalidator/issues/215): Add IMEI (International Mobile Station Equipment Identity) validator
 * [#216](https://github.com/nghuuphuoc/bootstrapvalidator/issues/216): Add ISMN (International Standard Music Number) validator
@@ -11,6 +12,7 @@
 
 ## v0.4.3 (2014-04-26)
 
+* Add ```$.fn.bootstrapValidator.helpers.luhn``` method that implements the Luhn algorithm
 * [#77](https://github.com/nghuuphuoc/bootstrapvalidator/issues/77): Add [```file``` validator](http://bootstrapvalidator.com/validators/file/)
 * [#179](https://github.com/nghuuphuoc/bootstrapvalidator/issues/179): Add [```vat``` validator](http://bootstrapvalidator.com/validators/vat/), support 32 countries
 * [#198](https://github.com/nghuuphuoc/bootstrapvalidator/pull/198): Add Canadian Postal Code support for the [```zipCode``` validator](http://bootstrapvalidator.com/validators/zip-code/), thanks to [@Francismori7](https://github.com/Francismori7)

+ 18 - 25
dist/js/bootstrapValidator.js

@@ -803,7 +803,7 @@
          * Credit to https://gist.github.com/ShirtlessKirk/2134376
          *
          * @param {String} value
-         * @returns {boolean}
+         * @returns {Boolean}
          */
         luhn: function(value) {
             var length  = value.length,
@@ -817,6 +817,21 @@
             }
 
             return (sum % 10 === 0 && sum > 0);
+        },
+
+        /**
+         * Implement modulus 11, 10 (ISO 7064) algorithm
+         *
+         * @param {String} value
+         * @returns {Boolean}
+         */
+        mod_11_10: function(value) {
+            var check  = 5,
+                length = value.length;
+            for (var i = 0; i < length; i++) {
+                check = (((check || 10) * 2) % 11 + parseInt(value.charAt(i), 10)) % 10;
+            }
+            return (check == 1);
         }
     };
 }(window.jQuery));
@@ -2932,18 +2947,7 @@
             }
 
             value = value.substr(2);
-            var product = 10,
-                sum     = 0;
-            for (var i = 0; i < 8; i++) {
-                sum = (parseInt(value.charAt(i), 10) + product) % 10;
-                if (sum == 0) {
-                    sum = 10;
-                }
-                product = (sum * 2) % 11;
-            }
-
-            var checkDigit = (11 - product == 10) ? 0 : (11 - product);
-            return (checkDigit == value.substr(8, 1));
+            return $.fn.bootstrapValidator.helpers.mod_11_10(value);
         },
 
         /**
@@ -3257,18 +3261,7 @@
             }
 
             value = value.substr(2);
-            var sum  = 10,
-                temp = 0;
-
-            for (var i = 0; i < 10; i++) {
-                temp = (parseInt(value.charAt(i), 10) + sum) % 10;
-                if (temp == 0) {
-                    temp = 10;
-                }
-                sum = (temp * 2) % 11;
-            }
-            sum += parseInt(value.substr(10, 1), 10);
-            return (sum % 10 == 1);
+            return $.fn.bootstrapValidator.helpers.mod_11_10(value);
         },
 
         /**

文件差异内容过多而无法显示
+ 2 - 2
dist/js/bootstrapValidator.min.js


+ 16 - 1
src/js/bootstrapValidator.js

@@ -802,7 +802,7 @@
          * Credit to https://gist.github.com/ShirtlessKirk/2134376
          *
          * @param {String} value
-         * @returns {boolean}
+         * @returns {Boolean}
          */
         luhn: function(value) {
             var length  = value.length,
@@ -816,6 +816,21 @@
             }
 
             return (sum % 10 === 0 && sum > 0);
+        },
+
+        /**
+         * Implement modulus 11, 10 (ISO 7064) algorithm
+         *
+         * @param {String} value
+         * @returns {Boolean}
+         */
+        mod_11_10: function(value) {
+            var check  = 5,
+                length = value.length;
+            for (var i = 0; i < length; i++) {
+                check = (((check || 10) * 2) % 11 + parseInt(value.charAt(i), 10)) % 10;
+            }
+            return (check == 1);
         }
     };
 }(window.jQuery));

+ 2 - 24
src/js/validator/vat.js

@@ -369,18 +369,7 @@
             }
 
             value = value.substr(2);
-            var product = 10,
-                sum     = 0;
-            for (var i = 0; i < 8; i++) {
-                sum = (parseInt(value.charAt(i), 10) + product) % 10;
-                if (sum == 0) {
-                    sum = 10;
-                }
-                product = (sum * 2) % 11;
-            }
-
-            var checkDigit = (11 - product == 10) ? 0 : (11 - product);
-            return (checkDigit == value.substr(8, 1));
+            return $.fn.bootstrapValidator.helpers.mod_11_10(value);
         },
 
         /**
@@ -694,18 +683,7 @@
             }
 
             value = value.substr(2);
-            var sum  = 10,
-                temp = 0;
-
-            for (var i = 0; i < 10; i++) {
-                temp = (parseInt(value.charAt(i), 10) + sum) % 10;
-                if (temp == 0) {
-                    temp = 10;
-                }
-                sum = (temp * 2) % 11;
-            }
-            sum += parseInt(value.substr(10, 1), 10);
-            return (sum % 10 == 1);
+            return $.fn.bootstrapValidator.helpers.mod_11_10(value);
         },
 
         /**