浏览代码

#320: Add separator option to the date validator

phuoc 11 年之前
父节点
当前提交
5fe110c561
共有 5 个文件被更改,包括 38 次插入10 次删除
  1. 1 0
      CHANGELOG.md
  2. 15 0
      demo/date.html
  3. 10 4
      dist/js/bootstrapValidator.js
  4. 2 2
      dist/js/bootstrapValidator.min.js
  5. 10 4
      src/js/validator/date.js

文件差异内容过多而无法显示
+ 1 - 0
CHANGELOG.md


+ 15 - 0
demo/date.html

@@ -53,6 +53,13 @@
                         </div>
 
                         <div class="form-group">
+                            <label class="col-lg-3 control-label">YYYY.MM.DD</label>
+                            <div class="col-lg-5">
+                                <input type="text" class="form-control" name="yearMonthDay" />
+                            </div>
+                        </div>
+
+                        <div class="form-group">
                             <label class="col-lg-3 control-label">MM/DD/YYYY h:m A</label>
                             <div class="col-lg-5">
                                 <input type="text" class="form-control" name="monthDayYearTime" />
@@ -115,6 +122,14 @@ $(document).ready(function() {
                     }
                 }
             },
+            yearMonthDay: {
+                validators: {
+                    date: {
+                        format: 'YYYY.MM.DD',
+                        separator: '.'
+                    }
+                }
+            },
             monthDayYearTime: {
                 validators: {
                     date: {

+ 10 - 4
dist/js/bootstrapValidator.js

@@ -1759,7 +1759,8 @@
     $.fn.bootstrapValidator.validators.date = {
         html5Attributes: {
             message: 'message',
-            format: 'format'
+            format: 'format',
+            separator: 'separator'
         },
 
         /**
@@ -1769,10 +1770,12 @@
          * @param {jQuery} $field Field element
          * @param {Object} options Can consist of the following keys:
          * - message: The invalid message
+         * - separator: Use to separate the date, month, and year.
+         * By default, it is /
          * - format: The date format. Default is MM/DD/YYYY
          * The format can be:
          *
-         * i) date: Consist of DD, MM, YYYY parts which are separated by /
+         * i) date: Consist of DD, MM, YYYY parts which are separated by the separator option
          * ii) date and time:
          * The time can consist of h, m, s parts which are separated by :
          * ii) date, time and A (indicating AM or PM)
@@ -1799,8 +1802,11 @@
             }
 
             // Determine the separator
-            var separator = (date.indexOf('/') != -1) ? '/' : ((date.indexOf('-') != -1) ? '-' : null);
-            if (separator == null) {
+            var separator = options.separator;
+            if (!separator) {
+                separator = (date.indexOf('/') != -1) ? '/' : ((date.indexOf('-') != -1) ? '-' : null);
+            }
+            if (separator == null || date.indexOf(separator) == -1) {
                 return false;
             }
 

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


+ 10 - 4
src/js/validator/date.js

@@ -2,7 +2,8 @@
     $.fn.bootstrapValidator.validators.date = {
         html5Attributes: {
             message: 'message',
-            format: 'format'
+            format: 'format',
+            separator: 'separator'
         },
 
         /**
@@ -12,10 +13,12 @@
          * @param {jQuery} $field Field element
          * @param {Object} options Can consist of the following keys:
          * - message: The invalid message
+         * - separator: Use to separate the date, month, and year.
+         * By default, it is /
          * - format: The date format. Default is MM/DD/YYYY
          * The format can be:
          *
-         * i) date: Consist of DD, MM, YYYY parts which are separated by /
+         * i) date: Consist of DD, MM, YYYY parts which are separated by the separator option
          * ii) date and time:
          * The time can consist of h, m, s parts which are separated by :
          * ii) date, time and A (indicating AM or PM)
@@ -42,8 +45,11 @@
             }
 
             // Determine the separator
-            var separator = (date.indexOf('/') != -1) ? '/' : ((date.indexOf('-') != -1) ? '-' : null);
-            if (separator == null) {
+            var separator = options.separator;
+            if (!separator) {
+                separator = (date.indexOf('/') != -1) ? '/' : ((date.indexOf('-') != -1) ? '-' : null);
+            }
+            if (separator == null || date.indexOf(separator) == -1) {
                 return false;
             }