ソースを参照

#215: Add IMEI (International Mobile Station Equipment Identity) validator

phuoc 11 年 前
コミット
38806d2702

+ 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
+* [#215](https://github.com/nghuuphuoc/bootstrapvalidator/issues/215): Add IMEI (International Mobile Station Equipment Identity) 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

+ 41 - 0
dist/js/bootstrapValidator.js

@@ -1723,6 +1723,47 @@
     };
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.imei = {
+        /**
+         * Validate IMEI (International Mobile Station Equipment Identity)
+         * Examples:
+         * - Valid: 35-209900-176148-1, 35-209900-176148-23, 3568680000414120, 490154203237518
+         * - Invalid: 490154203237517
+         *
+         * @see http://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity
+         * @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;
+            }
+
+            switch (true) {
+                case /^\d{15}$/.test(value):
+                case /^\d{2}-\d{6}-\d{6}-\d{1}$/.test(value):
+                case /^\d{2}\s\d{6}\s\d{6}\s\d{1}$/.test(value):
+                    value = value.replace(/[^0-9]/g, '');
+                    return $.fn.bootstrapValidator.helpers.luhn(value);
+                    break;
+
+                case /^\d{14}$/.test(value):
+                case /^\d{16}$/.test(value):
+                case /^\d{2}-\d{6}-\d{6}(|-\d{2})$/.test(value):
+                case /^\d{2}\s\d{6}\s\d{6}(|\s\d{2})$/.test(value):
+                    return true;
+
+                default:
+                    return false;
+            }
+        }
+    };
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.integer = {
         enableByHtml5: function($field) {
             return ('number' == $field.attr('type'));

ファイルの差分が大きいため隠しています
+ 2 - 2
dist/js/bootstrapValidator.min.js


+ 41 - 0
src/js/validator/imei.js

@@ -0,0 +1,41 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.imei = {
+        /**
+         * Validate IMEI (International Mobile Station Equipment Identity)
+         * Examples:
+         * - Valid: 35-209900-176148-1, 35-209900-176148-23, 3568680000414120, 490154203237518
+         * - Invalid: 490154203237517
+         *
+         * @see http://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity
+         * @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;
+            }
+
+            switch (true) {
+                case /^\d{15}$/.test(value):
+                case /^\d{2}-\d{6}-\d{6}-\d{1}$/.test(value):
+                case /^\d{2}\s\d{6}\s\d{6}\s\d{1}$/.test(value):
+                    value = value.replace(/[^0-9]/g, '');
+                    return $.fn.bootstrapValidator.helpers.luhn(value);
+                    break;
+
+                case /^\d{14}$/.test(value):
+                case /^\d{16}$/.test(value):
+                case /^\d{2}-\d{6}-\d{6}(|-\d{2})$/.test(value):
+                case /^\d{2}\s\d{6}\s\d{6}(|\s\d{2})$/.test(value):
+                    return true;
+
+                default:
+                    return false;
+            }
+        }
+    };
+}(window.jQuery));