浏览代码

#210: isbn validator accepts letters, special characters

nghuuphuoc 11 年之前
父节点
当前提交
aa628bfbb7
共有 4 个文件被更改,包括 76 次插入29 次删除
  1. 2 1
      CHANGELOG.md
  2. 36 13
      dist/js/bootstrapValidator.js
  3. 2 2
      dist/js/bootstrapValidator.min.js
  4. 36 13
      src/js/validator/isbn.js

+ 2 - 1
CHANGELOG.md

@@ -3,10 +3,11 @@
 ## v0.4.3 (not released yet)
 
 * [#77](https://github.com/nghuuphuoc/bootstrapvalidator/issues/77): Add ```file``` validator
-* [#179](https://github.com/nghuuphuoc/bootstrapvalidator/issues/179): Add ```vat``` validator
+* [#179](https://github.com/nghuuphuoc/bootstrapvalidator/issues/179): Add ```vat``` validator, support 32 countries
 * [#198](https://github.com/nghuuphuoc/bootstrapvalidator/pull/198): Add Canadian Postal Code support for the [```zipCode``` validator](http://bootstrapvalidator.com/validators/zip-code/), thanks to [@Francismori7](https://github.com/Francismori7)
 * [#202](https://github.com/nghuuphuoc/bootstrapvalidator/issues/202): Activate tab containing the first invalid field
 * [#205](https://github.com/nghuuphuoc/bootstrapvalidator/issues/205): Plugin method invocation
+* [#210](https://github.com/nghuuphuoc/bootstrapvalidator/issues/210): ```isbn``` validator accepts letters and special characters
 
 ## v0.4.2 (2014-04-19)
 

+ 36 - 13
dist/js/bootstrapValidator.js

@@ -1743,6 +1743,13 @@
     $.fn.bootstrapValidator.validators.isbn = {
         /**
          * Return true if the input value is a valid ISBN 10 or ISBN 13 number
+         * Examples:
+         * - Valid:
+         * ISBN 10: 99921-58-10-7, 9971-5-0210-0, 960-425-059-0, 80-902734-1-6, 85-359-0277-5, 1-84356-028-3, 0-684-84328-5, 0-8044-2957-X, 0-85131-041-9, 0-943396-04-2, 0-9752298-0-X
+         * ISBN 13: 978-0-306-40615-7
+         * - Invalid:
+         * ISBN 10: 99921-58-10-6
+         * ISBN 13: 978-0-306-40615-6
          *
          * @param {BootstrapValidator} validator The validator plugin instance
          * @param {jQuery} $field Field element
@@ -1756,18 +1763,35 @@
                 return true;
             }
 
+            // http://en.wikipedia.org/wiki/International_Standard_Book_Number#Overview
+            // Groups are separated by a hyphen or a space
+            var type;
+            switch (true) {
+                case /^\d{9}[\dX]$/.test(value):
+                case (value.length == 13 && /^(\d+)-(\d+)-(\d+)-([\dX])$/.test(value)):
+                case (value.length == 13 && /^(\d+)\s(\d+)\s(\d+)\s([\dX])$/.test(value)):
+                    type = 'ISBN10';
+                    break;
+                case /^(978|979)\d{9}[\dX]$/.test(value):
+                case (value.length == 17 && /^(978|979)-(\d+)-(\d+)-(\d+)-([\dX])$/.test(value)):
+                case (value.length == 17 && /^(978|979)\s(\d+)\s(\d+)\s(\d+)\s([\dX])$/.test(value)):
+                    type = 'ISBN13';
+                    break;
+                default:
+                    return false;
+            }
+
             // Replace all special characters except digits and X
-            value = value.replace(/[^\dX]/gi, '');
-            var chars = value.split(''),
-                sum   = 0,
+            value = value.replace(/[^0-9X]/gi, '');
+            var chars  = value.split(''),
+                length = chars.length,
+                sum    = 0,
                 checksum;
 
-            // See http://en.wikipedia.org/wiki/International_Standard_Book_Number
-            switch (chars.length) {
-                // ISBN 10
-                case 10:
+            switch (type) {
+                case 'ISBN10':
                     sum = 0;
-                    for (var i = 0; i < 9; i++) {
+                    for (var i = 0; i < length - 1; i++) {
                         sum += ((10 - i) * parseInt(chars[i]));
                     }
                     checksum = 11 - (sum % 11);
@@ -1776,19 +1800,18 @@
                     } else if (checksum == 10) {
                         checksum = 'X';
                     }
-                    return (checksum == chars[9]);
+                    return (checksum + '' == chars[length - 1]);
 
-                // ISBN 13
-                case 13:
+                case 'ISBN13':
                     sum = 0;
-                    for (var i = 0; i < 12; i++) {
+                    for (var i = 0; i < length - 1; i++) {
                         sum += ((i % 2 == 0) ? parseInt(chars[i]) : (parseInt(chars[i]) * 3));
                     }
                     checksum = 10 - (sum % 10);
                     if (checksum == 10) {
                         checksum = '0';
                     }
-                    return (checksum == chars[12]);
+                    return (checksum + '' == chars[length - 1]);
 
                 default:
                     return false;

文件差异内容过多而无法显示
+ 2 - 2
dist/js/bootstrapValidator.min.js


+ 36 - 13
src/js/validator/isbn.js

@@ -2,6 +2,13 @@
     $.fn.bootstrapValidator.validators.isbn = {
         /**
          * Return true if the input value is a valid ISBN 10 or ISBN 13 number
+         * Examples:
+         * - Valid:
+         * ISBN 10: 99921-58-10-7, 9971-5-0210-0, 960-425-059-0, 80-902734-1-6, 85-359-0277-5, 1-84356-028-3, 0-684-84328-5, 0-8044-2957-X, 0-85131-041-9, 0-943396-04-2, 0-9752298-0-X
+         * ISBN 13: 978-0-306-40615-7
+         * - Invalid:
+         * ISBN 10: 99921-58-10-6
+         * ISBN 13: 978-0-306-40615-6
          *
          * @param {BootstrapValidator} validator The validator plugin instance
          * @param {jQuery} $field Field element
@@ -15,18 +22,35 @@
                 return true;
             }
 
+            // http://en.wikipedia.org/wiki/International_Standard_Book_Number#Overview
+            // Groups are separated by a hyphen or a space
+            var type;
+            switch (true) {
+                case /^\d{9}[\dX]$/.test(value):
+                case (value.length == 13 && /^(\d+)-(\d+)-(\d+)-([\dX])$/.test(value)):
+                case (value.length == 13 && /^(\d+)\s(\d+)\s(\d+)\s([\dX])$/.test(value)):
+                    type = 'ISBN10';
+                    break;
+                case /^(978|979)\d{9}[\dX]$/.test(value):
+                case (value.length == 17 && /^(978|979)-(\d+)-(\d+)-(\d+)-([\dX])$/.test(value)):
+                case (value.length == 17 && /^(978|979)\s(\d+)\s(\d+)\s(\d+)\s([\dX])$/.test(value)):
+                    type = 'ISBN13';
+                    break;
+                default:
+                    return false;
+            }
+
             // Replace all special characters except digits and X
-            value = value.replace(/[^\dX]/gi, '');
-            var chars = value.split(''),
-                sum   = 0,
+            value = value.replace(/[^0-9X]/gi, '');
+            var chars  = value.split(''),
+                length = chars.length,
+                sum    = 0,
                 checksum;
 
-            // See http://en.wikipedia.org/wiki/International_Standard_Book_Number
-            switch (chars.length) {
-                // ISBN 10
-                case 10:
+            switch (type) {
+                case 'ISBN10':
                     sum = 0;
-                    for (var i = 0; i < 9; i++) {
+                    for (var i = 0; i < length - 1; i++) {
                         sum += ((10 - i) * parseInt(chars[i]));
                     }
                     checksum = 11 - (sum % 11);
@@ -35,19 +59,18 @@
                     } else if (checksum == 10) {
                         checksum = 'X';
                     }
-                    return (checksum == chars[9]);
+                    return (checksum + '' == chars[length - 1]);
 
-                // ISBN 13
-                case 13:
+                case 'ISBN13':
                     sum = 0;
-                    for (var i = 0; i < 12; i++) {
+                    for (var i = 0; i < length - 1; i++) {
                         sum += ((i % 2 == 0) ? parseInt(chars[i]) : (parseInt(chars[i]) * 3));
                     }
                     checksum = 10 - (sum % 10);
                     if (checksum == 10) {
                         checksum = '0';
                     }
-                    return (checksum == chars[12]);
+                    return (checksum + '' == chars[length - 1]);
 
                 default:
                     return false;