ソースを参照

#214: Add GRId (Global Release Identifier) validator

phuoc 11 年 前
コミット
06ee333bf1

+ 2 - 0
CHANGELOG.md

@@ -3,7 +3,9 @@
 ## v0.4.4 (not released yet)
 
 * Add ```$.fn.bootstrapValidator.helpers.mod_11_10``` method that implements modulus 11, 10 (ISO 7064) algorithm. The helper is then reused in validating [German and Croatian VAT](http://bootstrapvalidator.com/validators/vat/) numbers
+* Add ```$.fn.bootstrapValidator.helpers.mod_37_36``` method that implements modulus 37, 36 (ISO 7064) algorithm, used in GRid validator
 * [#213](https://github.com/nghuuphuoc/bootstrapvalidator/issues/213): Add EAN (International Article Number) validator
+* [#214](https://github.com/nghuuphuoc/bootstrapvalidator/issues/214): Add GRId (Global Release Identifier) validator
 * [#215](https://github.com/nghuuphuoc/bootstrapvalidator/issues/215): Add IMEI (International Mobile Station Equipment Identity) validator
 * [#216](https://github.com/nghuuphuoc/bootstrapvalidator/issues/216): Add ISMN (International Standard Music Number) validator
 * [#217](https://github.com/nghuuphuoc/bootstrapvalidator/issues/217): Add ISSN (International Standard Serial Number) validator

+ 54 - 0
dist/js/bootstrapValidator.js

@@ -832,6 +832,27 @@
                 check = (((check || 10) * 2) % 11 + parseInt(value.charAt(i), 10)) % 10;
             }
             return (check == 1);
+        },
+
+        /**
+         * Implements Mod 37, 36 (ISO 7064) algorithm
+         * Usages:
+         * mod_37_36('A12425GABC1234002M')
+         * mod_37_36('002006673085', '0123456789')
+         *
+         * @param {String} value
+         * @param {String} alphabet
+         * @returns {Boolean}
+         */
+        mod_37_36: function(value, alphabet) {
+            alphabet = alphabet || '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
+            var modulus = alphabet.length,
+                length  = value.length,
+                check   = Math.floor(modulus / 2);
+            for (var i = 0; i < length; i++) {
+                check = (((check || modulus) * 2) % (modulus + 1) + alphabet.indexOf(value.charAt(i))) % modulus;
+            }
+            return (check == 1);
         }
     };
 }(window.jQuery));
@@ -1510,6 +1531,39 @@
     }
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.grid = {
+        /**
+         * Validate GRId (Global Release Identifier)
+         * Examples:
+         * - Valid: A12425GABC1234002M, A1-2425G-ABC1234002-M, A1 2425G ABC1234002 M, Grid:A1-2425G-ABC1234002-M
+         * - Invalid: A1-2425G-ABC1234002-Q
+         *
+         * @see http://en.wikipedia.org/wiki/Global_Release_Identifier
+         * @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;
+            }
+
+            value = value.toUpperCase();
+            if (!/^[GRID:]*([0-9A-Z]{2})[-\s]*([0-9A-Z]{5})[-\s]*([0-9A-Z]{10})[-\s]*([0-9A-Z]{1})$/g.test(value)) {
+                return false;
+            }
+            value = value.replace(/\s/g, '').replace(/-/g, '');
+            if ('GRID:' == value.substr(0, 5)) {
+                value = value.substr(5);
+            }
+            return $.fn.bootstrapValidator.helpers.mod_37_36(value);
+        }
+    };
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.hex = {
         /**
          * Return true if and only if the input value is a valid hexadecimal number

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


+ 21 - 0
src/js/bootstrapValidator.js

@@ -831,6 +831,27 @@
                 check = (((check || 10) * 2) % 11 + parseInt(value.charAt(i), 10)) % 10;
             }
             return (check == 1);
+        },
+
+        /**
+         * Implements Mod 37, 36 (ISO 7064) algorithm
+         * Usages:
+         * mod_37_36('A12425GABC1234002M')
+         * mod_37_36('002006673085', '0123456789')
+         *
+         * @param {String} value
+         * @param {String} alphabet
+         * @returns {Boolean}
+         */
+        mod_37_36: function(value, alphabet) {
+            alphabet = alphabet || '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
+            var modulus = alphabet.length,
+                length  = value.length,
+                check   = Math.floor(modulus / 2);
+            for (var i = 0; i < length; i++) {
+                check = (((check || modulus) * 2) % (modulus + 1) + alphabet.indexOf(value.charAt(i))) % modulus;
+            }
+            return (check == 1);
         }
     };
 }(window.jQuery));

+ 33 - 0
src/js/validator/grid.js

@@ -0,0 +1,33 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.grid = {
+        /**
+         * Validate GRId (Global Release Identifier)
+         * Examples:
+         * - Valid: A12425GABC1234002M, A1-2425G-ABC1234002-M, A1 2425G ABC1234002 M, Grid:A1-2425G-ABC1234002-M
+         * - Invalid: A1-2425G-ABC1234002-Q
+         *
+         * @see http://en.wikipedia.org/wiki/Global_Release_Identifier
+         * @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;
+            }
+
+            value = value.toUpperCase();
+            if (!/^[GRID:]*([0-9A-Z]{2})[-\s]*([0-9A-Z]{5})[-\s]*([0-9A-Z]{10})[-\s]*([0-9A-Z]{1})$/g.test(value)) {
+                return false;
+            }
+            value = value.replace(/\s/g, '').replace(/-/g, '');
+            if ('GRID:' == value.substr(0, 5)) {
+                value = value.substr(5);
+            }
+            return $.fn.bootstrapValidator.helpers.mod_37_36(value);
+        }
+    };
+}(window.jQuery));