浏览代码

#251: Add ```cusip``` (North American Securities) validator

nghuuphuoc 11 年之前
父节点
当前提交
6dac627553
共有 4 个文件被更改,包括 105 次插入2 次删除
  1. 1 0
      CHANGELOG.md
  2. 51 0
      dist/js/bootstrapValidator.js
  3. 2 2
      dist/js/bootstrapValidator.min.js
  4. 51 0
      src/js/validator/cusip.js

+ 1 - 0
CHANGELOG.md

@@ -7,6 +7,7 @@
 * [#232](https://github.com/nghuuphuoc/bootstrapvalidator/issues/232): Add ```id``` validator
 * [#242](https://github.com/nghuuphuoc/bootstrapvalidator/issues/242): Add ```separator``` option to the [```numeric``` validator](http://bootstrapvalidator.com/validators/numeric/)
 * [#248](https://github.com/nghuuphuoc/bootstrapvalidator/issues/248): Add ```isin``` (International Securities Identification Number) validator
+* [#251](https://github.com/nghuuphuoc/bootstrapvalidator/issues/251): Add ```cusip``` (North American Securities) validator
 * [#252](https://github.com/nghuuphuoc/bootstrapvalidator/issues/252): Add ```sedol``` (Stock Exchange Daily Official List) validator
 * The [```zipCode``` validator](http://bootstrapvalidator.com/validators/zipCode/) adds support for Italian, Dutch postcodes
 * [#245](https://github.com/nghuuphuoc/bootstrapvalidator/pull/245): The [```cvv``` validator](http://bootstrapvalidator.com/validators/cvv/) should support spaces in credit card, thanks to [@evilchili](https://github.com/evilchili)

+ 51 - 0
dist/js/bootstrapValidator.js

@@ -1184,6 +1184,57 @@
     };
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.cusip = {
+        /**
+         * Validate a CUSIP
+         * Examples:
+         * - Valid: 037833100, 931142103, 14149YAR8, 126650BG6
+         * - Invalid: 31430F200, 022615AC2
+         *
+         * @see http://en.wikipedia.org/wiki/CUSIP
+         * @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 (!/^[0-9A-Z]{9}$/.test(value)) {
+                return false;
+            }
+
+            var converted = $.map(value.split(''), function(item) {
+                                var code = item.charCodeAt(0);
+                                return (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0))
+                                            // Replace A, B, C, ..., Z with 10, 11, ..., 35
+                                            ? (code - 'A'.charCodeAt(0) + 10)
+                                            : item;
+                            }),
+                length    = converted.length,
+                sum       = 0;
+            for (var i = 0; i < length - 1; i++) {
+                var num = parseInt(converted[i]);
+                if (i % 2 != 0) {
+                    num *= 2;
+                }
+                if (num > 9) {
+                    num -= 9;
+                }
+                sum += num;
+            }
+
+            sum = (10 - (sum % 10)) % 10;
+            return sum == converted[length - 1];
+        }
+    };
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.cvv = {
         html5Attributes: {
             message: 'message',

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


+ 51 - 0
src/js/validator/cusip.js

@@ -0,0 +1,51 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.cusip = {
+        /**
+         * Validate a CUSIP
+         * Examples:
+         * - Valid: 037833100, 931142103, 14149YAR8, 126650BG6
+         * - Invalid: 31430F200, 022615AC2
+         *
+         * @see http://en.wikipedia.org/wiki/CUSIP
+         * @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 (!/^[0-9A-Z]{9}$/.test(value)) {
+                return false;
+            }
+
+            var converted = $.map(value.split(''), function(item) {
+                                var code = item.charCodeAt(0);
+                                return (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0))
+                                            // Replace A, B, C, ..., Z with 10, 11, ..., 35
+                                            ? (code - 'A'.charCodeAt(0) + 10)
+                                            : item;
+                            }),
+                length    = converted.length,
+                sum       = 0;
+            for (var i = 0; i < length - 1; i++) {
+                var num = parseInt(converted[i]);
+                if (i % 2 != 0) {
+                    num *= 2;
+                }
+                if (num > 9) {
+                    num -= 9;
+                }
+                sum += num;
+            }
+
+            sum = (10 - (sum % 10)) % 10;
+            return sum == converted[length - 1];
+        }
+    };
+}(window.jQuery));