Browse Source

#216: Add ISMN (International Standard Music Number) validator

phuoc 11 years ago
parent
commit
e9a9ed3989
4 changed files with 113 additions and 2 deletions
  1. 1 0
      CHANGELOG.md
  2. 55 0
      dist/js/bootstrapValidator.js
  3. 2 2
      dist/js/bootstrapValidator.min.js
  4. 55 0
      src/js/validator/ismn.js

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@
 ## v0.4.4 (not released yet)
 
 * [#213](https://github.com/nghuuphuoc/bootstrapvalidator/issues/213): Add EAN (International Article Number) 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
 * [#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
 

+ 55 - 0
dist/js/bootstrapValidator.js

@@ -1863,6 +1863,61 @@
     };
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.ismn = {
+        /**
+         * Validate ISMN (International Standard Music Number)
+         * Examples:
+         * - Valid: M230671187, 979-0-0601-1561-5, 979 0 3452 4680 5, 9790060115615
+         * - Invalid: 9790060115614
+         *
+         * @see http://en.wikipedia.org/wiki/International_Standard_Music_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;
+            }
+
+            // Groups are separated by a hyphen or a space
+            var type;
+            switch (true) {
+                case /^M\d{9}$/.test(value):
+                case /^M-\d{4}-\d{4}-\d{1}$/.test(value):
+                case /^M\s\d{4}\s\d{4}\s\d{1}$/.test(value):
+                    type = 'ISMN10';
+                    break;
+                case /^9790\d{9}$/.test(value):
+                case /^979-0-\d{4}-\d{4}-\d{1}$/.test(value):
+                case /^979\s0\s\d{4}\s\d{4}\s\d{1}$/.test(value):
+                    type = 'ISMN13';
+                    break;
+                default:
+                    return false;
+            }
+
+            if ('ISMN10' == type) {
+                value = '9790' + value.substr(1);
+            }
+
+            // Replace all special characters except digits
+            value = value.replace(/[^0-9]/gi, '');
+            var length = value.length,
+                sum    = 0,
+                weight = [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.issn = {
         /**
          * Validate ISSN (International Standard Serial Number)

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


+ 55 - 0
src/js/validator/ismn.js

@@ -0,0 +1,55 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.ismn = {
+        /**
+         * Validate ISMN (International Standard Music Number)
+         * Examples:
+         * - Valid: M230671187, 979-0-0601-1561-5, 979 0 3452 4680 5, 9790060115615
+         * - Invalid: 9790060115614
+         *
+         * @see http://en.wikipedia.org/wiki/International_Standard_Music_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;
+            }
+
+            // Groups are separated by a hyphen or a space
+            var type;
+            switch (true) {
+                case /^M\d{9}$/.test(value):
+                case /^M-\d{4}-\d{4}-\d{1}$/.test(value):
+                case /^M\s\d{4}\s\d{4}\s\d{1}$/.test(value):
+                    type = 'ISMN10';
+                    break;
+                case /^9790\d{9}$/.test(value):
+                case /^979-0-\d{4}-\d{4}-\d{1}$/.test(value):
+                case /^979\s0\s\d{4}\s\d{4}\s\d{1}$/.test(value):
+                    type = 'ISMN13';
+                    break;
+                default:
+                    return false;
+            }
+
+            if ('ISMN10' == type) {
+                value = '9790' + value.substr(1);
+            }
+
+            // Replace all special characters except digits
+            value = value.replace(/[^0-9]/gi, '');
+            var length = value.length,
+                sum    = 0,
+                weight = [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));