浏览代码

Credit card validator uses luhn helper

phuoc 11 年之前
父节点
当前提交
b66fc648ee
共有 2 个文件被更改,包括 2 次插入20 次删除
  1. 1 1
      src/js/bootstrapValidator.js
  2. 1 19
      src/js/validator/creditCard.js

+ 1 - 1
src/js/bootstrapValidator.js

@@ -791,7 +791,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

+ 1 - 19
src/js/validator/creditCard.js

@@ -22,25 +22,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;
             }