ソースを参照

Add regexp validator

phuoc 12 年 前
コミット
f56ccbfc77

+ 2 - 1
demo/index.html

@@ -14,6 +14,7 @@
     <script type="text/javascript" src="../src/js/bootstrapValidate.js"></script>
     <script type="text/javascript" src="../src/js/validator/notEmpty.js"></script>
     <script type="text/javascript" src="../src/js/validator/stringLength.js"></script>
+    <script type="text/javascript" src="../src/js/validator/regexp.js"></script>
 </head>
 <body>
     <div class="container">
@@ -61,7 +62,7 @@ $(document).ready(function() {
                         message: 'The username must be more than 6 and less than 30 characters long'
                     },
                     regexp: {
-                        reg: /^[a-zA-Z0-9_\.]+$/,
+                        regexp: /^[a-zA-Z0-9_\.]+$/,
                         message: 'The username can only consist of alphabetical, number and underscore'
                     }
                 }

+ 7 - 0
src/js/validator/notEmpty.js

@@ -1,6 +1,13 @@
 (function($) {
     $.extend($.bootstrapValidator.validator, {
         notEmpty: {
+            /**
+             * Check an input value is empty or not
+             *
+             * @param {HTMLElement} element
+             * @param {Object} options
+             * @returns {boolean}
+             */
             validate: function(element, options) {
                 var value = $.trim($(element).val());
                 return (value != '');

+ 17 - 0
src/js/validator/regexp.js

@@ -0,0 +1,17 @@
+(function($) {
+    $.extend($.bootstrapValidator.validator, {
+        regexp: {
+            /**
+             * Check the element value matches given regular expression
+             *
+             * @param {HTMLElement} element
+             * @param {Object} options
+             * @returns {boolean}
+             */
+            validate: function(element, options) {
+                var value = $.trim($(element).val());
+                return value.match(options.regexp);
+            }
+        }
+    });
+}(window.jQuery));

+ 7 - 0
src/js/validator/stringLength.js

@@ -1,6 +1,13 @@
 (function($) {
     $.extend($.bootstrapValidator.validator, {
         stringLength: {
+            /**
+             * Check the length of element value is less or more than given number
+             *
+             * @param {HTMLElement} element
+             * @param {Object} options
+             * @returns {boolean}
+             */
             validate: function(element, options) {
                 var value = $.trim($(element).val()), length = value.length;
                 if ((options.min && length < options.min) || (options.max && length > options.max)) {