Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
make date validator use the Date object
Browse files Browse the repository at this point in the history
  • Loading branch information
bolu authored and mhevery committed Sep 23, 2010
1 parent db42221 commit 0d5407b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ foreach({
return _null;
},

'date': function(value, min, max) {
if (value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/)) {
return _null;
}
return "Value is not a date. (Expecting format: 12/31/2009).";
'date': function(value) {
var fields = /^(\d\d?)\/(\d\d?)\/(\d\d\d\d)$/.exec(value);
var date = fields ? new Date(fields[3], fields[1]-1, fields[2]) : 0;
return (date &&
date.getFullYear() == fields[3] &&
date.getMonth() == fields[1]-1 &&
date.getDate() == fields[2]) ?
_null : "Value is not a date. (Expecting format: 12/31/2009).";
},

'ssn': function(value) {
Expand Down
15 changes: 13 additions & 2 deletions test/ValidatorsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,19 @@ ValidatorTest.prototype.testInteger = function() {

ValidatorTest.prototype.testDate = function() {
var error = "Value is not a date. (Expecting format: 12/31/2009).";
assertEquals(angular.validator.date("ab"), error);
assertEquals(angular.validator.date("12/31/2009"), null);
expect(angular.validator.date("ab")).toEqual(error);
expect(angular.validator.date("12/31/2009")).toEqual(null);
expect(angular.validator.date("1/1/1000")).toEqual(null);
expect(angular.validator.date("12/31/9999")).toEqual(null);
expect(angular.validator.date("2/29/2004")).toEqual(null);
expect(angular.validator.date("2/29/2000")).toEqual(null);
expect(angular.validator.date("2/29/2100")).toEqual(error);
expect(angular.validator.date("2/29/2003")).toEqual(error);
expect(angular.validator.date("41/1/2009")).toEqual(error);
expect(angular.validator.date("13/1/2009")).toEqual(error);
expect(angular.validator.date("1/1/209")).toEqual(error);
expect(angular.validator.date("1/32/2010")).toEqual(error);
expect(angular.validator.date("001/031/2009")).toEqual(error);
};

ValidatorTest.prototype.testPhone = function() {
Expand Down

0 comments on commit 0d5407b

Please sign in to comment.