Browse Source

#112: creditCard validator now also validates the card's CCV (prefix) and length

nghuuphuoc 12 years ago
parent
commit
6f5c46d49a
3 changed files with 147 additions and 5 deletions
  1. 73 2
      dist/js/bootstrapValidator.js
  2. 1 1
      dist/js/bootstrapValidator.min.js
  3. 73 2
      src/js/validator/creditCard.js

+ 73 - 2
dist/js/bootstrapValidator.js

@@ -665,10 +665,11 @@
             if (/[^0-9-\s]+/.test(value)) {
                 return false;
             }
+            value = value.replace(/\D/g, '');
 
+            // Validate the check sum
             // The Luhn Algorithm
             // http://en.wikipedia.org/wiki/Luhn
-            value = value.replace(/\D/g, '');
             var check = 0, digit = 0, even = false, length = value.length;
 
             for (var n = length - 1; n >= 0; n--) {
@@ -684,7 +685,77 @@
                 even = !even;
             }
 
-            return (check % 10) == 0;
+            if ((check % 10) != 0) {
+                return false;
+            }
+
+            // Validate the card number based on pattern
+            var cards = {
+                AMERICAN_EXPRESS: {
+                    length: [15],
+                    ccv: ['34', '37']
+                },
+                DINERS_CLUB: {
+                    length: [14],
+                    ccv: ['300', '301', '302', '303', '304', '305', '36']
+                },
+                DINERS_CLUB_US: {
+                    length: [16],
+                    ccv: ['54', '55']
+                },
+                DISCOVER: {
+                    length: [16],
+                    ccv: ['6011', '622126', '622127', '622128', '622129', '62213',
+                          '62214', '62215', '62216', '62217', '62218', '62219',
+                          '6222', '6223', '6224', '6225', '6226', '6227', '6228',
+                          '62290', '62291', '622920', '622921', '622922', '622923',
+                          '622924', '622925', '644', '645', '646', '647', '648',
+                          '649', '65']
+                },
+                JCB: {
+                    length: [16],
+                    ccv: ['3528', '3529', '353', '354', '355', '356', '357', '358']
+                },
+                LASER: {
+                    length: [16, 17, 18, 19],
+                    ccv: ['3528', '3529', '353', '354', '355', '356', '357', '358']
+                },
+                MAESTRO: {
+                    length: [12, 13, 14, 15, 16, 17, 18, 19],
+                    ccv: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
+                },
+                MASTERCARD: {
+                    length: [16],
+                    ccv: ['51', '52', '53', '54', '55']
+                },
+                SOLO: {
+                    length: [16, 18, 19],
+                    ccv: ['6334', '6767']
+                },
+                UNIONPAY: {
+                    length: [16, 17, 18, 19],
+                    ccv: ['622126', '622127', '622128', '622129', '62213', '62214',
+                          '62215', '62216', '62217', '62218', '62219', '6222', '6223',
+                          '6224', '6225', '6226', '6227', '6228', '62290', '62291',
+                          '622920', '622921', '622922', '622923', '622924', '622925']
+                },
+                VISA: {
+                    length: ['16'],
+                    ccv: ['4']
+                }
+            };
+            var type, prefix;
+            for (type in cards) {
+                for (prefix in cards[type]['ccv']) {
+                    if (value.substr(0, prefix.length) == prefix                // Check the prefix
+                        && cards[type]['length'].indexOf(value.length) != -1)   // and length
+                    {
+                        return true;
+                    }
+                }
+            }
+
+            return false;
         }
     };
 }(window.jQuery));

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


+ 73 - 2
src/js/validator/creditCard.js

@@ -20,10 +20,11 @@
             if (/[^0-9-\s]+/.test(value)) {
                 return false;
             }
+            value = value.replace(/\D/g, '');
 
+            // Validate the check sum
             // The Luhn Algorithm
             // http://en.wikipedia.org/wiki/Luhn
-            value = value.replace(/\D/g, '');
             var check = 0, digit = 0, even = false, length = value.length;
 
             for (var n = length - 1; n >= 0; n--) {
@@ -39,7 +40,77 @@
                 even = !even;
             }
 
-            return (check % 10) == 0;
+            if ((check % 10) != 0) {
+                return false;
+            }
+
+            // Validate the card number based on pattern
+            var cards = {
+                AMERICAN_EXPRESS: {
+                    length: [15],
+                    ccv: ['34', '37']
+                },
+                DINERS_CLUB: {
+                    length: [14],
+                    ccv: ['300', '301', '302', '303', '304', '305', '36']
+                },
+                DINERS_CLUB_US: {
+                    length: [16],
+                    ccv: ['54', '55']
+                },
+                DISCOVER: {
+                    length: [16],
+                    ccv: ['6011', '622126', '622127', '622128', '622129', '62213',
+                          '62214', '62215', '62216', '62217', '62218', '62219',
+                          '6222', '6223', '6224', '6225', '6226', '6227', '6228',
+                          '62290', '62291', '622920', '622921', '622922', '622923',
+                          '622924', '622925', '644', '645', '646', '647', '648',
+                          '649', '65']
+                },
+                JCB: {
+                    length: [16],
+                    ccv: ['3528', '3529', '353', '354', '355', '356', '357', '358']
+                },
+                LASER: {
+                    length: [16, 17, 18, 19],
+                    ccv: ['3528', '3529', '353', '354', '355', '356', '357', '358']
+                },
+                MAESTRO: {
+                    length: [12, 13, 14, 15, 16, 17, 18, 19],
+                    ccv: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
+                },
+                MASTERCARD: {
+                    length: [16],
+                    ccv: ['51', '52', '53', '54', '55']
+                },
+                SOLO: {
+                    length: [16, 18, 19],
+                    ccv: ['6334', '6767']
+                },
+                UNIONPAY: {
+                    length: [16, 17, 18, 19],
+                    ccv: ['622126', '622127', '622128', '622129', '62213', '62214',
+                          '62215', '62216', '62217', '62218', '62219', '6222', '6223',
+                          '6224', '6225', '6226', '6227', '6228', '62290', '62291',
+                          '622920', '622921', '622922', '622923', '622924', '622925']
+                },
+                VISA: {
+                    length: ['16'],
+                    ccv: ['4']
+                }
+            };
+            var type, prefix;
+            for (type in cards) {
+                for (prefix in cards[type]['ccv']) {
+                    if (value.substr(0, prefix.length) == prefix                // Check the prefix
+                        && cards[type]['length'].indexOf(value.length) != -1)   // and length
+                    {
+                        return true;
+                    }
+                }
+            }
+
+            return false;
         }
     };
 }(window.jQuery));