Browse Source

#217: Add ISSN (International Standard Serial Number) validator

phuoc 12 years ago
parent
commit
7d45bc4d79
4 changed files with 87 additions and 2 deletions
  1. 1 0
      CHANGELOG.md
  2. 42 0
      dist/js/bootstrapValidator.js
  3. 2 2
      dist/js/bootstrapValidator.min.js
  4. 42 0
      src/js/validator/issn.js

+ 1 - 0
CHANGELOG.md

@@ -2,6 +2,7 @@
 
 ## v0.4.4 (not released yet)
 
+* [#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
 
 ## v0.4.3 (2014-04-26)

+ 42 - 0
dist/js/bootstrapValidator.js

@@ -1826,6 +1826,48 @@
     };
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.issn = {
+        /**
+         * Validate ISSN (International Standard Serial Number)
+         * Examples:
+         * - Valid: 0378-5955, 0024-9319, 0032-1478
+         * - Invalid: 0032-147X
+         *
+         * @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;
+            }
+
+            // 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;
+            }
+
+            // Replace all special characters except digits and X
+            value = value.replace(/[^0-9X]/gi, '');
+            var chars  = value.split(''),
+                length = chars.length,
+                sum    = 0;
+
+            if (chars[7] == 'X') {
+                chars[7] = 10;
+            }
+            for (var i = 0; i < length; i++) {
+                sum += ((8 - i) * parseInt(chars[i]));
+            }
+            return (sum % 11 == 0);
+        }
+    };
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.lessThan = {
         html5Attributes: {
             message: 'message',

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


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

@@ -0,0 +1,42 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.issn = {
+        /**
+         * Validate ISSN (International Standard Serial Number)
+         * Examples:
+         * - Valid: 0378-5955, 0024-9319, 0032-1478
+         * - Invalid: 0032-147X
+         *
+         * @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;
+            }
+
+            // 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;
+            }
+
+            // Replace all special characters except digits and X
+            value = value.replace(/[^0-9X]/gi, '');
+            var chars  = value.split(''),
+                length = chars.length,
+                sum    = 0;
+
+            if (chars[7] == 'X') {
+                chars[7] = 10;
+            }
+            for (var i = 0; i < length; i++) {
+                sum += ((8 - i) * parseInt(chars[i]));
+            }
+            return (sum % 11 == 0);
+        }
+    };
+}(window.jQuery));