Browse Source

#97: Fix CVV validator

nghuuphuoc 12 years ago
parent
commit
b96dc47cd3
4 changed files with 22 additions and 18 deletions
  1. 3 7
      demo/selector.html
  2. 9 5
      dist/js/bootstrapValidator.js
  3. 1 1
      dist/js/bootstrapValidator.min.js
  4. 9 5
      src/js/validator/cvv.js

+ 3 - 7
demo/selector.html

@@ -119,13 +119,9 @@ $(document).ready(function() {
                     notEmpty: {
                         message: 'The CVV number is required'
                     },
-                    digits: {
-                        message: 'The CVV number can contain digits only'
-                    },
-                    stringLength: {
-                        min: 3,
-                        max: 4,
-                        message: 'Invalid CVV number'
+                    cvv: {
+                        message: 'The value is not a valid CVV',
+                        creditCardField: 'ccNumber'
                     }
                 }
             }

+ 9 - 5
dist/js/bootstrapValidator.js

@@ -787,6 +787,9 @@
 
             // Get the credit card number
             var creditCard = validator.getFieldElements(options.creditCardField).val();
+            if (creditCard == '') {
+                return true;
+            }
 
             // Supported credit card types
             var cards = {
@@ -839,17 +842,18 @@
                           '622920', '622921', '622922', '622923', '622924', '622925']
                 },
                 VISA: {
-                    length: ['16'],
+                    length: [16],
                     ccv: ['4']
                 }
             };
-            var type, prefix, creditCardType = null;
+            var type, i, creditCardType = null;
             for (type in cards) {
-                for (prefix in cards[type]['ccv']) {
-                    if (creditCard.substr(0, prefix.length) == prefix                // Check the prefix
-                        && cards[type]['length'].indexOf(creditCard.length) != -1)   // and length
+                for (i in cards[type]['ccv']) {
+                    if (creditCard.substr(0, cards[type]['ccv'][i].length) == cards[type]['ccv'][i] // Check the prefix
+                        && cards[type]['length'].indexOf(creditCard.length) != -1)                  // and length
                     {
                         creditCardType = type;
+                        break;
                     }
                 }
             }

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


+ 9 - 5
src/js/validator/cvv.js

@@ -26,6 +26,9 @@
 
             // Get the credit card number
             var creditCard = validator.getFieldElements(options.creditCardField).val();
+            if (creditCard == '') {
+                return true;
+            }
 
             // Supported credit card types
             var cards = {
@@ -78,17 +81,18 @@
                           '622920', '622921', '622922', '622923', '622924', '622925']
                 },
                 VISA: {
-                    length: ['16'],
+                    length: [16],
                     ccv: ['4']
                 }
             };
-            var type, prefix, creditCardType = null;
+            var type, i, creditCardType = null;
             for (type in cards) {
-                for (prefix in cards[type]['ccv']) {
-                    if (creditCard.substr(0, prefix.length) == prefix                // Check the prefix
-                        && cards[type]['length'].indexOf(creditCard.length) != -1)   // and length
+                for (i in cards[type]['ccv']) {
+                    if (creditCard.substr(0, cards[type]['ccv'][i].length) == cards[type]['ccv'][i] // Check the prefix
+                        && cards[type]['length'].indexOf(creditCard.length) != -1)                  // and length
                     {
                         creditCardType = type;
+                        break;
                     }
                 }
             }