ソースを参照

#213: Add EAN (International Article Number) validator

phuoc 12 年 前
コミット
b49b0ef60e

+ 1 - 0
CHANGELOG.md

@@ -2,6 +2,7 @@
 
 ## v0.4.4 (not released yet)
 
+* [#213](https://github.com/nghuuphuoc/bootstrapvalidator/issues/213): Add EAN (International Article Number) validator
 * [#217](https://github.com/nghuuphuoc/bootstrapvalidator/issues/217): Add ISSN (International Standard Serial Number) validator
 * [#191](https://github.com/nghuuphuoc/bootstrapvalidator/issues/191), [#223](https://github.com/nghuuphuoc/bootstrapvalidator/issues/223): Support using both the ```name``` attribute and ```selector``` option for field
 

+ 38 - 1
dist/js/bootstrapValidator.js

@@ -1338,6 +1338,42 @@
     }
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.ean = {
+        /**
+         * Validate EAN (International Article Number)
+         * Examples:
+         * - Valid: 73513537, 9780471117094, 4006381333931
+         * - Invalid: 73513536
+         *
+         * @see http://en.wikipedia.org/wiki/European_Article_Number
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Can consist of the following keys:
+         * - message: The invalid message
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            if (!/^(\d{8}|\d{12}|\d{13})$/.test(value)) {
+                return false;
+            }
+
+            var length = value.length,
+                sum    = 0,
+                weight = (length == 8) ? [3, 1] : [1, 3];
+            for (var i = 0; i < length - 1; i++) {
+                sum += parseInt(value.charAt(i)) * weight[i % 2];
+            }
+            sum = 10 - sum % 10;
+            return (sum == value.charAt(length - 1));
+        }
+    };
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.emailAddress = {
         enableByHtml5: function($field) {
             return ('email' == $field.attr('type'));
@@ -1757,6 +1793,7 @@
          * ISBN 10: 99921-58-10-6
          * ISBN 13: 978-0-306-40615-6
          *
+         * @see http://en.wikipedia.org/wiki/International_Standard_Book_Number
          * @param {BootstrapValidator} validator The validator plugin instance
          * @param {jQuery} $field Field element
          * @param {Object} options Can consist of the following keys:
@@ -1833,6 +1870,7 @@
          * - Valid: 0378-5955, 0024-9319, 0032-1478
          * - Invalid: 0032-147X
          *
+         * @see http://en.wikipedia.org/wiki/International_Standard_Serial_Number
          * @param {BootstrapValidator} validator The validator plugin instance
          * @param {jQuery} $field Field element
          * @param {Object} options Can consist of the following keys:
@@ -1845,7 +1883,6 @@
                 return true;
             }
 
-            // http://en.wikipedia.org/wiki/International_Standard_Serial_Number
             // Groups are separated by a hyphen or a space
             if (!/^\d{4}\-\d{3}[\dX]$/.test(value)) {
                 return false;

ファイルの差分が大きいため隠しています
+ 1 - 1
dist/js/bootstrapValidator.min.js


+ 36 - 0
src/js/validator/ean.js

@@ -0,0 +1,36 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.ean = {
+        /**
+         * Validate EAN (International Article Number)
+         * Examples:
+         * - Valid: 73513537, 9780471117094, 4006381333931
+         * - Invalid: 73513536
+         *
+         * @see http://en.wikipedia.org/wiki/European_Article_Number
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Can consist of the following keys:
+         * - message: The invalid message
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            if (!/^(\d{8}|\d{12}|\d{13})$/.test(value)) {
+                return false;
+            }
+
+            var length = value.length,
+                sum    = 0,
+                weight = (length == 8) ? [3, 1] : [1, 3];
+            for (var i = 0; i < length - 1; i++) {
+                sum += parseInt(value.charAt(i)) * weight[i % 2];
+            }
+            sum = 10 - sum % 10;
+            return (sum == value.charAt(length - 1));
+        }
+    };
+}(window.jQuery));

+ 1 - 0
src/js/validator/isbn.js

@@ -10,6 +10,7 @@
          * ISBN 10: 99921-58-10-6
          * ISBN 13: 978-0-306-40615-6
          *
+         * @see http://en.wikipedia.org/wiki/International_Standard_Book_Number
          * @param {BootstrapValidator} validator The validator plugin instance
          * @param {jQuery} $field Field element
          * @param {Object} options Can consist of the following keys:

+ 1 - 1
src/js/validator/issn.js

@@ -6,6 +6,7 @@
          * - Valid: 0378-5955, 0024-9319, 0032-1478
          * - Invalid: 0032-147X
          *
+         * @see http://en.wikipedia.org/wiki/International_Standard_Serial_Number
          * @param {BootstrapValidator} validator The validator plugin instance
          * @param {jQuery} $field Field element
          * @param {Object} options Can consist of the following keys:
@@ -18,7 +19,6 @@
                 return true;
             }
 
-            // http://en.wikipedia.org/wiki/International_Standard_Serial_Number
             // Groups are separated by a hyphen or a space
             if (!/^\d{4}\-\d{3}[\dX]$/.test(value)) {
                 return false;