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

Commit 0d5407b

Browse files
bolumhevery
authored andcommitted
make date validator use the Date object
1 parent db42221 commit 0d5407b

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/validators.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ foreach({
3434
return _null;
3535
},
3636

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

4447
'ssn': function(value) {

test/ValidatorsTest.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,19 @@ ValidatorTest.prototype.testInteger = function() {
4646

4747
ValidatorTest.prototype.testDate = function() {
4848
var error = "Value is not a date. (Expecting format: 12/31/2009).";
49-
assertEquals(angular.validator.date("ab"), error);
50-
assertEquals(angular.validator.date("12/31/2009"), null);
49+
expect(angular.validator.date("ab")).toEqual(error);
50+
expect(angular.validator.date("12/31/2009")).toEqual(null);
51+
expect(angular.validator.date("1/1/1000")).toEqual(null);
52+
expect(angular.validator.date("12/31/9999")).toEqual(null);
53+
expect(angular.validator.date("2/29/2004")).toEqual(null);
54+
expect(angular.validator.date("2/29/2000")).toEqual(null);
55+
expect(angular.validator.date("2/29/2100")).toEqual(error);
56+
expect(angular.validator.date("2/29/2003")).toEqual(error);
57+
expect(angular.validator.date("41/1/2009")).toEqual(error);
58+
expect(angular.validator.date("13/1/2009")).toEqual(error);
59+
expect(angular.validator.date("1/1/209")).toEqual(error);
60+
expect(angular.validator.date("1/32/2010")).toEqual(error);
61+
expect(angular.validator.date("001/031/2009")).toEqual(error);
5162
};
5263

5364
ValidatorTest.prototype.testPhone = function() {

0 commit comments

Comments
 (0)