浏览代码

#116: Add uuid validator

nghuuphuoc 11 年之前
父节点
当前提交
15e3237f3d
共有 3 个文件被更改,包括 73 次插入1 次删除
  1. 36 0
      dist/js/bootstrapValidator.js
  2. 1 1
      dist/js/bootstrapValidator.min.js
  3. 36 0
      src/js/validator/uuid.js

+ 36 - 0
dist/js/bootstrapValidator.js

@@ -1905,6 +1905,42 @@
     };
     };
 }(window.jQuery));
 }(window.jQuery));
 ;(function($) {
 ;(function($) {
+    $.fn.bootstrapValidator.validators.uuid = {
+        html5Attributes: {
+            message: 'message',
+            version: 'version'
+        },
+
+        /**
+         * Return true if and only if the input value is a valid UUID string
+         *
+         * @see http://en.wikipedia.org/wiki/Universally_unique_identifier
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Consist of key:
+         * - message: The invalid message
+         * - version: Can be 3, 4, 5, null
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            // See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions
+            var patterns = {
+                    '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
+                    '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
+                    '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
+                    all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
+                },
+                version = options.version ? (options.version + '') : 'all';
+            return (null == patterns[version]) ? true : patterns[version].test(value);
+        }
+    };
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.zipCode = {
     $.fn.bootstrapValidator.validators.zipCode = {
         html5Attributes: {
         html5Attributes: {
             message: 'message',
             message: 'message',

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


+ 36 - 0
src/js/validator/uuid.js

@@ -0,0 +1,36 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.uuid = {
+        html5Attributes: {
+            message: 'message',
+            version: 'version'
+        },
+
+        /**
+         * Return true if and only if the input value is a valid UUID string
+         *
+         * @see http://en.wikipedia.org/wiki/Universally_unique_identifier
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Consist of key:
+         * - message: The invalid message
+         * - version: Can be 3, 4, 5, null
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            // See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions
+            var patterns = {
+                    '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
+                    '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
+                    '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
+                    all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
+                },
+                version = options.version ? (options.version + '') : 'all';
+            return (null == patterns[version]) ? true : patterns[version].test(value);
+        }
+    };
+}(window.jQuery));