Browse Source

Add regexp validator

phuoc 12 years ago
parent
commit
f56ccbfc77
4 changed files with 33 additions and 1 deletions
  1. 2 1
      demo/index.html
  2. 7 0
      src/js/validator/notEmpty.js
  3. 17 0
      src/js/validator/regexp.js
  4. 7 0
      src/js/validator/stringLength.js

+ 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)) {