validator.js icon indicating copy to clipboard operation
validator.js copied to clipboard

validator.isDate seems to return true for just about anything

Open seanwendt opened this issue 10 years ago • 2 comments

> var x = 99999;
> validator.isDate(x);
true

> var x = "99999";
> validator.isDate(x);
true

> var x = 0.99999
> validator.isDate(x);
false

> var x = 0.1
> validator.isDate(x);
true

> var x = "foo";
> validator.isDate(x);
false

seanwendt avatar Jan 22 '16 03:01 seanwendt

I believe it requires validator.toDate for string sanitization? e.g.

str = validator.toDate(str); validator.isDate(str);

seanwendt avatar Jan 22 '16 03:01 seanwendt

It doesn't require sanitization with toDate first. The isDate validator uses the built-in Date.parse() function which is quite lenient with what it accepts.

You can see what it's doing by calling new Date(Date.parse(str)), e.g.

> new Date(Date.parse('0-5-23'));
Tue May 23 2000 00:00:00 GMT+0900 (JST)
> new Date(Date.parse('0-5'));
Mon May 01 2000 00:00:00 GMT+0900 (JST)
> new Date(Date.parse('0'));
Sat Jan 01 2000 00:00:00 GMT+0900 (JST)
> new Date(Date.parse('0.1'));
Sat Jan 01 2000 00:00:00 GMT+0900 (JST)

If you need stricter date validation, I suggest using isISO8601 or regular expressions.

chriso avatar Jan 27 '16 02:01 chriso