Browse Source

#9: Add credit card validator

nghuuphuoc 12 years ago
parent
commit
643a8cd2e3
4 changed files with 89 additions and 5 deletions
  1. 5 4
      README.md
  2. 41 0
      dist/js/bootstrapValidator.js
  3. 1 1
      dist/js/bootstrapValidator.min.js
  4. 42 0
      src/js/validator/creditCard.js

+ 5 - 4
README.md

@@ -48,9 +48,9 @@ $(document).ready(function() {
 
         // Custom submit handler
         // The handler has two arguments
-        // - The ```validator``` is the instance of ```BootstrapValidator```
-        // - The ```form``` is jQuery object representing the current form
-        // By default, ```submitHandler``` is ```null```
+        // - validator is the instance of BootstrapValidator
+        // - form is jQuery object representing the current form
+        // By default, submitHandler is null
         submitHandler: function(validator, form) {
         },
 
@@ -84,6 +84,7 @@ Below is the list of built-in validators sorted in alphabetical order:
 Validator name                          | Description
 ----------------------------------------|------------
 [between](#between-validator)           | Check if the input value is between (strictly or not) two given numbers
+creditCard                              | Validate a credit card number
 [different](#different-validator)       | Return true if the input value is different with given field's value
 digits                                  | Return true if the value contains only digits
 emailAddress                            | Validate an email address
@@ -100,7 +101,7 @@ usZipCode                               | Validate a US zip code
 
 The validator options are described in the following section:
 
-**the option masked with * is required**
+(**The options masked with * are required**)
 
 ### Between Validator
 

+ 41 - 0
dist/js/bootstrapValidator.js

@@ -339,6 +339,47 @@
     };
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.creditCard = {
+        /**
+         * Return true if the input value is valid credit card number
+         * Based on https://gist.github.com/DiegoSalazar/4075533
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Can consist of the following key:
+         * - message: The invalid message
+         * @returns {boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+
+            // Accept only digits, dashes or spaces
+            if (/[^0-9-\s]+/.test(value)) {
+                return false;
+            }
+
+            // 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--) {
+                digit = parseInt(value.charAt(n), 10);
+
+                if (even) {
+                    if ((digit *= 2) > 9) {
+                        digit -= 9;
+                    }
+                }
+
+                check += digit;
+                even = !even;
+            }
+
+            return (check % 10) == 0;
+        }
+    };
+}(window.jQuery));;(function($) {
     $.fn.bootstrapValidator.validators.different = {
         /**
          * Return true if the input value is different with given field's value

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


+ 42 - 0
src/js/validator/creditCard.js

@@ -0,0 +1,42 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.creditCard = {
+        /**
+         * Return true if the input value is valid credit card number
+         * Based on https://gist.github.com/DiegoSalazar/4075533
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Can consist of the following key:
+         * - message: The invalid message
+         * @returns {boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+
+            // Accept only digits, dashes or spaces
+            if (/[^0-9-\s]+/.test(value)) {
+                return false;
+            }
+
+            // 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--) {
+                digit = parseInt(value.charAt(n), 10);
+
+                if (even) {
+                    if ((digit *= 2) > 9) {
+                        digit -= 9;
+                    }
+                }
+
+                check += digit;
+                even = !even;
+            }
+
+            return (check % 10) == 0;
+        }
+    };
+}(window.jQuery));