From 0d5407bc1e976f1f9908d01cfdf848b6c212d135 Mon Sep 17 00:00:00 2001 From: Bolek Szewczyk Date: Thu, 23 Sep 2010 13:48:17 +0200 Subject: [PATCH] make date validator use the Date object --- src/validators.js | 13 ++++++++----- test/ValidatorsTest.js | 15 +++++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/validators.js b/src/validators.js index b99c8aa9ed82..3eb11f785c98 100644 --- a/src/validators.js +++ b/src/validators.js @@ -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) { diff --git a/test/ValidatorsTest.js b/test/ValidatorsTest.js index 2e156f8467b6..ca8dad12724d 100644 --- a/test/ValidatorsTest.js +++ b/test/ValidatorsTest.js @@ -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() {