Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validator.isDate seems to return true for just about anything #476

Closed
seanwendt opened this issue Jan 22, 2016 · 2 comments
Closed

validator.isDate seems to return true for just about anything #476

seanwendt opened this issue Jan 22, 2016 · 2 comments

Comments

@seanwendt
Copy link

> 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
Copy link
Author

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

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

@chriso
Copy link
Collaborator

chriso commented Jan 27, 2016

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 chriso closed this as completed Jan 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants