Skip to content

Commit

Permalink
Refactored toDate() validator to pass the test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Branko Vukelic committed Aug 4, 2011
1 parent 07636a3 commit b6a2950
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,10 @@ Validator.prototype.isUUID = function(version) {
}

Validator.prototype.isDate = function() {
var pattern = /^([0-1]{0,1}[0-9]{1})\/([0-3]{0,1}[0-9]{1})\/([0-9]{4})$/;
var result = pattern.exec(this.str);

if (!result || result.length != 4 ) {
return this.error(this.msg || 'Not a date');
var intDate = Date.parse(this.str);
if (isNaN(intDate)) {
return this.error(this.msg || 'Not a date');
}

var dt = new Date(this.str);

if ( dt.getFullYear() != parseInt(result[3])
|| dt.getMonth() + 1 != parseInt(result[1])
|| dt.getDate() != parseInt(result[2])
) {
return this.error(this.msg || 'Not a date');
}

return this;
}

Expand Down

0 comments on commit b6a2950

Please sign in to comment.