phuoc 11 年之前
父节点
当前提交
b6769f7ec2

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

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.0-dev, built on 2014-07-06 10:28:36 PM
+ * @version     v0.5.0-dev, built on 2014-07-07 6:50:25 PM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 8 - 8
dist/js/bootstrapValidator.js

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.0-dev, built on 2014-07-06 10:28:36 PM
+ * @version     v0.5.0-dev, built on 2014-07-07 6:50:25 PM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -362,10 +362,10 @@
             switch (true) {
                 case (!!options.message):
                     return options.message;
-                case (!!$.fn.bootstrapValidator.i18n[validatorName]):
-                    return $.fn.bootstrapValidator.i18n[validatorName]['default'];
                 case (!!this.options.fields[field].message):
                     return this.options.fields[field].message;
+                case (!!$.fn.bootstrapValidator.i18n[validatorName]):
+                    return $.fn.bootstrapValidator.i18n[validatorName]['default'];
                 default:
                     return this.options.message;
             }
@@ -1686,6 +1686,10 @@
                 return false;
             }
 
+            day   = parseInt(day, 10);
+            month = parseInt(month, 10);
+            year  = parseInt(year, 10);
+
             if (year < 1000 || year > 9999 || month === 0 || month > 12) {
                 return false;
             }
@@ -1696,7 +1700,7 @@
             }
 
             // Check the day
-            if (day < 0 || day > numDays[month - 1]) {
+            if (day <= 0 || day > numDays[month - 1]) {
                 return false;
             }
 
@@ -2351,10 +2355,6 @@
             }
 
             // Validate day, month, and year
-            day   = parseInt(day, 10);
-            month = parseInt(month, 10);
-            year  = parseInt(year, 10);
-
             return $.fn.bootstrapValidator.helpers.date(year, month, day);
         }
     };

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


+ 7 - 3
src/js/bootstrapValidator.js

@@ -361,10 +361,10 @@
             switch (true) {
                 case (!!options.message):
                     return options.message;
-                case (!!$.fn.bootstrapValidator.i18n[validatorName]):
-                    return $.fn.bootstrapValidator.i18n[validatorName]['default'];
                 case (!!this.options.fields[field].message):
                     return this.options.fields[field].message;
+                case (!!$.fn.bootstrapValidator.i18n[validatorName]):
+                    return $.fn.bootstrapValidator.i18n[validatorName]['default'];
                 default:
                     return this.options.message;
             }
@@ -1685,6 +1685,10 @@
                 return false;
             }
 
+            day   = parseInt(day, 10);
+            month = parseInt(month, 10);
+            year  = parseInt(year, 10);
+
             if (year < 1000 || year > 9999 || month === 0 || month > 12) {
                 return false;
             }
@@ -1695,7 +1699,7 @@
             }
 
             // Check the day
-            if (day < 0 || day > numDays[month - 1]) {
+            if (day <= 0 || day > numDays[month - 1]) {
                 return false;
             }
 

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

@@ -104,10 +104,6 @@
             }
 
             // Validate day, month, and year
-            day   = parseInt(day, 10);
-            month = parseInt(month, 10);
-            year  = parseInt(year, 10);
-
             return $.fn.bootstrapValidator.helpers.date(year, month, day);
         }
     };

+ 88 - 0
test/spec.js

@@ -1736,6 +1736,94 @@ describe('creditCard', function() {
     });
 });
 
+describe('date', function() {
+    beforeEach(function () {
+        $([
+            '<form class="form-horizontal" id="dateForm">',
+                '<div id="msg"></div>',
+                '<div class="form-group">',
+                    '<input type="text" name="date" data-bv-date />',
+                '</div>',
+            '</form>'
+        ].join('\n')).appendTo('body');
+
+        $('#dateForm').bootstrapValidator();
+
+        this.bv    = $('#dateForm').data('bootstrapValidator');
+        this.$date = this.bv.getFieldElements('date');
+    });
+
+    afterEach(function () {
+        $('#dateForm').bootstrapValidator('destroy').remove();
+    });
+
+    it('YYYY/MM/DD', function() {
+        this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD');
+
+        this.$date.val('2000/01/30');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+
+        // Invalid year
+        this.bv.resetForm();
+        this.$date.val('100/10/20');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        // Invalid month
+        this.bv.resetForm();
+        this.$date.val('2000/00/10');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('2000/15/10');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        // Invalid day
+        this.bv.resetForm();
+        this.$date.val('2000/03/00');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('2000/10/32');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        // Consist invalid characters
+        // #310
+        this.bv.resetForm();
+        this.$date.val('aaaa/');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('2004df/1dd1/5ffg');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+    });
+
+    it('number of days in February', function() {
+        this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD');
+
+        this.$date.val('2000/02/28');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+
+        this.bv.resetForm();
+        this.$date.val('2000/02/29');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+
+        this.bv.resetForm();
+        this.$date.val('2001/02/29');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+    });
+});
+
 describe('ean', function() {
     beforeEach(function() {
         var html = [

+ 87 - 0
test/spec/validator/date.js

@@ -0,0 +1,87 @@
+describe('date', function() {
+    beforeEach(function () {
+        $([
+            '<form class="form-horizontal" id="dateForm">',
+                '<div id="msg"></div>',
+                '<div class="form-group">',
+                    '<input type="text" name="date" data-bv-date />',
+                '</div>',
+            '</form>'
+        ].join('\n')).appendTo('body');
+
+        $('#dateForm').bootstrapValidator();
+
+        this.bv    = $('#dateForm').data('bootstrapValidator');
+        this.$date = this.bv.getFieldElements('date');
+    });
+
+    afterEach(function () {
+        $('#dateForm').bootstrapValidator('destroy').remove();
+    });
+
+    it('YYYY/MM/DD', function() {
+        this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD');
+
+        this.$date.val('2000/01/30');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+
+        // Invalid year
+        this.bv.resetForm();
+        this.$date.val('100/10/20');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        // Invalid month
+        this.bv.resetForm();
+        this.$date.val('2000/00/10');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('2000/15/10');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        // Invalid day
+        this.bv.resetForm();
+        this.$date.val('2000/03/00');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('2000/10/32');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        // Consist invalid characters
+        // #310
+        this.bv.resetForm();
+        this.$date.val('aaaa/');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('2004df/1dd1/5ffg');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+    });
+
+    it('number of days in February', function() {
+        this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD');
+
+        this.$date.val('2000/02/28');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+
+        this.bv.resetForm();
+        this.$date.val('2000/02/29');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+
+        this.bv.resetForm();
+        this.$date.val('2001/02/29');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+    });
+});