ソースを参照

#681: Fix the date validator issue where one of date/month/year or hours/minutes/seconds is prefixed by zero

phuoc 11 年 前
コミット
cab23ce468

+ 1 - 0
CHANGELOG.md

@@ -30,6 +30,7 @@ __Bug Fixes__
 * [#550](https://github.com/nghuuphuoc/bootstrapvalidator/issues/550), [#551](https://github.com/nghuuphuoc/bootstrapvalidator/pull/551): Cannot validate against both ipv4 and ipv6 at the same time, thanks to [@beeglebug](https://github.com/beeglebug)
 * [#665](https://github.com/nghuuphuoc/bootstrapvalidator/issues/665): The [submitButtons](http://bootstrapvalidator.com/settings/#form-submit-buttons) option doesn't work correctly
 * [#672](https://github.com/nghuuphuoc/bootstrapvalidator/issues/672): The [zipCode](http://bootstrapvalidator.com/validators/zipCode/) validator throw an exception when passing not supported country code
+* [#681](https://github.com/nghuuphuoc/bootstrapvalidator/issues/681): Fix the [date](http://bootstrapvalidator.com/validators/date/) validator issue where one of date/month/year or hours/minutes/seconds is prefixed by zero
 
 __Language Packages__
 

+ 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.1-dev, built on 2014-08-16 5:21:21 PM
+ * @version     v0.5.1-dev, built on 2014-08-17 7:49:08 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 16 - 4
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.1-dev, built on 2014-08-16 5:21:21 PM
+ * @version     v0.5.1-dev, built on 2014-08-17 7:49:08 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -1772,6 +1772,9 @@
             if (isNaN(year) || isNaN(month) || isNaN(day)) {
                 return false;
             }
+            if (day.length > 2 || month.length > 2 || year.length !== 4) {
+                return false;
+            }
 
             day   = parseInt(day, 10);
             month = parseInt(month, 10);
@@ -2426,24 +2429,33 @@
 
                 // Validate seconds
                 if (seconds) {
+                    if (isNaN(seconds) || seconds.length > 2) {
+                        return false;
+                    }
                     seconds = parseInt(seconds, 10);
-                    if (isNaN(seconds) || seconds < 0 || seconds > 60) {
+                    if (seconds < 0 || seconds > 60) {
                         return false;
                     }
                 }
 
                 // Validate hours
                 if (hours) {
+                    if (isNaN(hours) || hours.length > 2) {
+                        return false;
+                    }
                     hours = parseInt(hours, 10);
-                    if (isNaN(hours) || hours < 0 || hours >= 24 || (amOrPm && hours > 12)) {
+                    if (hours < 0 || hours >= 24 || (amOrPm && hours > 12)) {
                         return false;
                     }
                 }
 
                 // Validate minutes
                 if (minutes) {
+                    if (isNaN(minutes) || minutes.length > 2) {
+                        return false;
+                    }
                     minutes = parseInt(minutes, 10);
-                    if (isNaN(minutes) || minutes < 0 || minutes > 59) {
+                    if (minutes < 0 || minutes > 59) {
                         return false;
                     }
                 }

ファイルの差分が大きいため隠しています
+ 3 - 3
dist/js/bootstrapValidator.min.js


+ 3 - 0
src/js/bootstrapValidator.js

@@ -1771,6 +1771,9 @@
             if (isNaN(year) || isNaN(month) || isNaN(day)) {
                 return false;
             }
+            if (day.length > 2 || month.length > 2 || year.length !== 4) {
+                return false;
+            }
 
             day   = parseInt(day, 10);
             month = parseInt(month, 10);

+ 12 - 3
src/js/validator/date.js

@@ -88,24 +88,33 @@
 
                 // Validate seconds
                 if (seconds) {
+                    if (isNaN(seconds) || seconds.length > 2) {
+                        return false;
+                    }
                     seconds = parseInt(seconds, 10);
-                    if (isNaN(seconds) || seconds < 0 || seconds > 60) {
+                    if (seconds < 0 || seconds > 60) {
                         return false;
                     }
                 }
 
                 // Validate hours
                 if (hours) {
+                    if (isNaN(hours) || hours.length > 2) {
+                        return false;
+                    }
                     hours = parseInt(hours, 10);
-                    if (isNaN(hours) || hours < 0 || hours >= 24 || (amOrPm && hours > 12)) {
+                    if (hours < 0 || hours >= 24 || (amOrPm && hours > 12)) {
                         return false;
                     }
                 }
 
                 // Validate minutes
                 if (minutes) {
+                    if (isNaN(minutes) || minutes.length > 2) {
+                        return false;
+                    }
                     minutes = parseInt(minutes, 10);
-                    if (isNaN(minutes) || minutes < 0 || minutes > 59) {
+                    if (minutes < 0 || minutes > 59) {
                         return false;
                     }
                 }

+ 51 - 4
test/spec.js

@@ -2128,7 +2128,7 @@ describe('creditCard', function() {
 });
 
 describe('date', function() {
-    beforeEach(function () {
+    beforeEach(function() {
         $([
             '<form class="form-horizontal" id="dateForm">',
                 '<div id="msg"></div>',
@@ -2144,7 +2144,7 @@ describe('date', function() {
         this.$date = this.bv.getFieldElements('date');
     });
 
-    afterEach(function () {
+    afterEach(function() {
         $('#dateForm').bootstrapValidator('destroy').remove();
     });
 
@@ -2200,7 +2200,7 @@ describe('date', function() {
         expect(this.bv.isValid()).toEqual(false);
 
         // Consist invalid characters
-        // #310
+        // Issue #310
         this.bv.resetForm();
         this.$date.val('aaaa/');
         this.bv.validate();
@@ -2211,7 +2211,7 @@ describe('date', function() {
         this.bv.validate();
         expect(this.bv.isValid()).toEqual(false);
 
-        // #475
+        // Issue #475
         this.bv.resetForm();
         this.$date.val('2014/09');
         this.bv.validate();
@@ -2268,6 +2268,53 @@ describe('date', function() {
         this.bv.validate();
         expect(this.bv.isValid()).toEqual(false);
     });
+
+    // Issue #681
+    it('date, month, year are prefixed by zero', function() {
+        this.bv.updateOption('date', 'date', 'format', 'MM/DD/YYYY');
+
+        this.$date.val('0012/08/2014');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('12/0008/2014');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('12/08/002014');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('12/08/2014');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+    });
+
+    it('hours, minutes, seconds are prefixed by zero', function() {
+        this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD h:m:s');
+
+        this.$date.val('2014/08/17 0007:30:00');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('2014/08/17 07:030:00');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('2014/08/17 07:30:0000');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('2014/08/17 07:30:00');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+    });
 });
 
 describe('ean', function() {

+ 51 - 4
test/spec/validator/date.js

@@ -1,5 +1,5 @@
 describe('date', function() {
-    beforeEach(function () {
+    beforeEach(function() {
         $([
             '<form class="form-horizontal" id="dateForm">',
                 '<div id="msg"></div>',
@@ -15,7 +15,7 @@ describe('date', function() {
         this.$date = this.bv.getFieldElements('date');
     });
 
-    afterEach(function () {
+    afterEach(function() {
         $('#dateForm').bootstrapValidator('destroy').remove();
     });
 
@@ -71,7 +71,7 @@ describe('date', function() {
         expect(this.bv.isValid()).toEqual(false);
 
         // Consist invalid characters
-        // #310
+        // Issue #310
         this.bv.resetForm();
         this.$date.val('aaaa/');
         this.bv.validate();
@@ -82,7 +82,7 @@ describe('date', function() {
         this.bv.validate();
         expect(this.bv.isValid()).toEqual(false);
 
-        // #475
+        // Issue #475
         this.bv.resetForm();
         this.$date.val('2014/09');
         this.bv.validate();
@@ -139,4 +139,51 @@ describe('date', function() {
         this.bv.validate();
         expect(this.bv.isValid()).toEqual(false);
     });
+
+    // Issue #681
+    it('date, month, year are prefixed by zero', function() {
+        this.bv.updateOption('date', 'date', 'format', 'MM/DD/YYYY');
+
+        this.$date.val('0012/08/2014');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('12/0008/2014');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('12/08/002014');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('12/08/2014');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+    });
+
+    it('hours, minutes, seconds are prefixed by zero', function() {
+        this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD h:m:s');
+
+        this.$date.val('2014/08/17 0007:30:00');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('2014/08/17 07:030:00');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('2014/08/17 07:30:0000');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$date.val('2014/08/17 07:30:00');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+    });
 });