浏览代码

#4, #72, #79: Add date validator

nghuuphuoc 11 年之前
父节点
当前提交
77f1ee7ed5

+ 15 - 0
demo/index.html

@@ -74,6 +74,13 @@
                         </div>
 
                         <div class="form-group">
+                            <label class="col-lg-3 control-label">Birthday</label>
+                            <div class="col-lg-5">
+                                <input type="text" class="form-control" name="birthday" /> (YYYY/MM/DD)
+                            </div>
+                        </div>
+
+                        <div class="form-group">
                             <label class="col-lg-3 control-label">Languages</label>
                             <div class="col-lg-5">
                                 <div class="checkbox">
@@ -250,6 +257,14 @@ $(document).ready(function() {
                     }
                 }
             },
+            birthday: {
+                validators: {
+                    date: {
+                        format: 'YYYY/MM/DD',
+                        message: 'The birthday is not valid'
+                    }
+                }
+            },
             gender: {
                 validators: {
                     notEmpty: {

+ 1 - 1
dist/css/bootstrapValidator.min.css

@@ -3,7 +3,7 @@
  *
  * A jQuery plugin to validate form fields. Use with Bootstrap 3
  *
- * @version     v0.3.0
+ * @version     v0.3.1-dev
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 75 - 2
dist/js/bootstrapValidator.js

@@ -3,7 +3,7 @@
  *
  * A jQuery plugin to validate form fields. Use with Bootstrap 3
  *
- * @version     v0.3.0
+ * @version     v0.3.1-dev
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -442,7 +442,7 @@
             }
         },
 
-        // Useful APIs which aren't use internally
+        // Useful APIs which aren't used internally
 
         /**
          * Reset the form
@@ -628,6 +628,79 @@
     };
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.date = {
+        /**
+         * Return true if the input value is valid date
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Can consist of the following keys:
+         * - format: The date format.
+         * It is a combination of YYYY, MM, DD, and separators (which can be / or -)
+         * Default is MM/DD/YYYY
+         * - message: The invalid message
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+            options.format = options.format || 'MM/DD/YYYY';
+
+            var separator = (options.format.indexOf('/') == -1) ? '-' : '/',
+                parts     = value.split(separator);
+            if (parts.length != 3) {
+                return false;
+            }
+
+            var d, m, y;
+            switch (options.format.toUpperCase().replace(/-/g, '/')) {
+                case 'YYYY/MM/DD':
+                    d = parts[2];
+                    m = parts[1];
+                    y = parts[0];
+                    break;
+                case 'YYYY/DD/MM':
+                    d = parts[1];
+                    m = parts[2];
+                    y = parts[0];
+                    break;
+                case 'DD/MM/YYYY':
+                    d = parts[0];
+                    m = parts[1];
+                    y = parts[2];
+                    break;
+                case 'MM/DD/YYYY':
+                    d = parts[1];
+                    m = parts[0];
+                    y = parts[2];
+                    break;
+                default:
+                    return false;
+            }
+
+            d = parseInt(d, 10);
+            m = parseInt(m, 10);
+            y = parseInt(y, 10);
+
+            if (y < 1000 || y > 9999 || m == 0 || m > 12) {
+                return false;
+            }
+
+            var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
+            // Update the number of days in Feb of leap year
+            if (y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)) {
+                numDays[1] = 29;
+            }
+
+            // Check the day
+            return (d > 0 && d <= numDays[m - 1]);
+        }
+    };
+}(window.jQuery));
+
+;(function($) {
     $.fn.bootstrapValidator.validators.different = {
         /**
          * Return true if the input value is different with given field's value

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


+ 1 - 1
package.json

@@ -13,5 +13,5 @@
     "grunt-contrib-uglify": "~0.4.0",
     "grunt-contrib-watch": "~0.5.3"
   },
-  "version": "0.3.0"
+  "version": "0.3.1-dev"
 }

+ 1 - 1
src/js/bootstrapValidator.js

@@ -441,7 +441,7 @@
             }
         },
 
-        // Useful APIs which aren't use internally
+        // Useful APIs which aren't used internally
 
         /**
          * Reset the form

+ 73 - 0
src/js/validator/date.js

@@ -0,0 +1,73 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.date = {
+        /**
+         * Return true if the input value is valid date
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Can consist of the following keys:
+         * - format: The date format.
+         * It is a combination of YYYY, MM, DD, and separators (which can be / or -)
+         * Default is MM/DD/YYYY
+         * - message: The invalid message
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+            options.format = options.format || 'MM/DD/YYYY';
+
+            var separator = (options.format.indexOf('/') == -1) ? '-' : '/',
+                parts     = value.split(separator);
+            if (parts.length != 3) {
+                return false;
+            }
+
+            var d, m, y;
+            switch (options.format.toUpperCase().replace(/-/g, '/')) {
+                case 'YYYY/MM/DD':
+                    d = parts[2];
+                    m = parts[1];
+                    y = parts[0];
+                    break;
+                case 'YYYY/DD/MM':
+                    d = parts[1];
+                    m = parts[2];
+                    y = parts[0];
+                    break;
+                case 'DD/MM/YYYY':
+                    d = parts[0];
+                    m = parts[1];
+                    y = parts[2];
+                    break;
+                case 'MM/DD/YYYY':
+                    d = parts[1];
+                    m = parts[0];
+                    y = parts[2];
+                    break;
+                default:
+                    return false;
+            }
+
+            d = parseInt(d, 10);
+            m = parseInt(m, 10);
+            y = parseInt(y, 10);
+
+            if (y < 1000 || y > 9999 || m == 0 || m > 12) {
+                return false;
+            }
+
+            var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
+            // Update the number of days in Feb of leap year
+            if (y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)) {
+                numDays[1] = 29;
+            }
+
+            // Check the day
+            return (d > 0 && d <= numDays[m - 1]);
+        }
+    };
+}(window.jQuery));
+