浏览代码

#177: Add Vehicle Identification Number (VIN) validator

nghuuphuoc 11 年之前
父节点
当前提交
82fff95409
共有 2 个文件被更改,包括 46 次插入0 次删除
  1. 1 0
      CHANGELOG.md
  2. 45 0
      src/js/validator/vin.js

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@
 ## v0.4.2 (not released yet)
 
 * [#168](https://github.com/nghuuphuoc/bootstrapvalidator/pull/168): Add siren and siret validators, thanks to [@jswale](https://github.com/jswale)
+* [#177](https://github.com/nghuuphuoc/bootstrapvalidator/issues/177): Add Vehicle Identification Number (VIN) validator
 * [#171](https://github.com/nghuuphuoc/bootstrapvalidator/pull/171): The [```phone``` validator](http://bootstrapvalidator.com/validators/phone/) now supports +1 country code and area code for US phone number, thanks to [@tomByrer](https://github.com/tomByrer)
 * [#173](https://github.com/nghuuphuoc/bootstrapvalidator/pull/173): The [```remote``` validator](http://bootstrapvalidator.com/validators/remote/) allows to override ```name``` option, thanks to [@jswale](https://github.com/jswale)
 * [#178](https://github.com/nghuuphuoc/bootstrapvalidator/pull/178): Do not validate fields that ```enabled``` is set to ```false```, thanks to [@henningda](https://github.com/henningda)

+ 45 - 0
src/js/validator/vin.js

@@ -0,0 +1,45 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.vin = {
+        /**
+         * Validate an US VIN (Vehicle Identification Number)
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Consist of key:
+         * - message: The invalid message
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '' || !options.country) {
+                return true;
+            }
+
+            // Don't accept I, O, Q characters
+            if (!/^[a-hj-npr-z0-9]{8}[0-9xX][a-hj-npr-z0-9]{8}$/i.test(value)) {
+                return false;
+            }
+
+            value = value.toUpperCase();
+            var chars   = {
+                    A: 1,   B: 2,   C: 3,   D: 4,   E: 5,   F: 6,   G: 7,   H: 8,
+                    J: 1,   K: 2,   L: 3,   M: 4,   N: 5,           P: 7,           R: 9,
+                            S: 2,   T: 3,   U: 4,   V: 5,   W: 6,   X: 7,   Y: 8,   Z: 9,
+                    '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 0
+                },
+                weights = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2],
+                sum     = 0,
+                length  = value.length;
+            for (var i = 0; i < length; i++) {
+                sum += chars[value[i] + ''] * weights[i];
+            }
+
+            var reminder = sum % 11;
+            if (reminder == 10) {
+                reminder = 'X';
+            }
+
+            return reminder == value[8];
+        }
+    };
+}(window.jQuery));