Skip to content

Commit e7f5762

Browse files
committed
Updated isDate() thanks to user foxbunny
1 parent 140b847 commit e7f5762

File tree

5 files changed

+21
-32
lines changed

5 files changed

+21
-32
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ Validator.prototype.getErrors = function () {
178178
- [PING](https://github.com/PlNG) - Fixed entity encoding
179179
- [Dan VerWeire](https://github.com/wankdanker) - Modified the behaviour of the error handler
180180
- [ctavan](https://github.com/ctavan) - Added isArray and isUUID()
181-
- [foxbunny](https://github.com/foxbunny) - Added min() and max()
181+
- [foxbunny](https://github.com/foxbunny) - Added min() and max(), and improved isDate()
182182
- [oris](https://github.com/orls) - Addded in()
183183

184184
## LICENSE

lib/validator.js

+3-15
Original file line numberDiff line numberDiff line change
@@ -191,22 +191,10 @@ Validator.prototype.isUUID = function(version) {
191191
}
192192

193193
Validator.prototype.isDate = function() {
194-
var pattern = /^([0-1]{0,1}[0-9]{1})\/([0-3]{0,1}[0-9]{1})\/([0-9]{4})$/;
195-
var result = pattern.exec(this.str);
196-
197-
if (!result || result.length != 4 ) {
198-
return this.error(this.msg || 'Not a date');
194+
var intDate = Date.parse(this.str);
195+
if (isNaN(intDate)) {
196+
return this.error(this.msg || 'Not a date');
199197
}
200-
201-
var dt = new Date(this.str);
202-
203-
if ( dt.getFullYear() != parseInt(result[3])
204-
|| dt.getMonth() + 1 != parseInt(result[1])
205-
|| dt.getDate() != parseInt(result[2])
206-
) {
207-
return this.error(this.msg || 'Not a date');
208-
}
209-
210198
return this;
211199
}
212200

test/validator.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -502,5 +502,18 @@ module.exports = {
502502
assert.ok(Validator.check(new Object()).isArray());
503503
assert.ok(false, 'len failed');
504504
} catch (e) {}
505+
},
506+
507+
'test #isDate()': function() {
508+
assert.ok(Validator.check('2011-08-04').isDate());
509+
assert.ok(Validator.check('04. 08. 2011.').isDate());
510+
assert.ok(Validator.check('08/04/2011').isDate());
511+
assert.ok(Validator.check('2011.08.04').isDate());
512+
assert.ok(Validator.check('4. 8. 2011. GMT').isDate());
513+
assert.ok(Validator.check('2011-08-04 12:00').isDate());
514+
515+
assert.throws(Validator.check('foo').isDate);
516+
assert.throws(Validator.check('2011-foo-04').isDate);
517+
assert.throws(Validator.check('GMT').isDate);
505518
}
506519
}

validator-min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

validator.js

+3-15
Original file line numberDiff line numberDiff line change
@@ -696,22 +696,10 @@
696696
}
697697

698698
Validator.prototype.isDate = function() {
699-
var pattern = /^([0-1]{0,1}[0-9]{1})\/([0-3]{0,1}[0-9]{1})\/([0-9]{4})$/;
700-
var result = pattern.exec(this.str);
701-
702-
if (result.length != 4 ) {
703-
return this.error(this.msg || 'Not a date');
699+
var intDate = Date.parse(this.str);
700+
if (isNaN(intDate)) {
701+
return this.error(this.msg || 'Not a date');
704702
}
705-
706-
var dt = new Date(this.str);
707-
708-
if ( dt.getFullYear() != parseInt(result[3])
709-
|| dt.getMonth() + 1 != parseInt(result[1])
710-
|| dt.getDate() != parseInt(result[2])
711-
) {
712-
return this.error(this.msg || 'Not a date');
713-
}
714-
715703
return this;
716704
}
717705

0 commit comments

Comments
 (0)