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

date validation before setting it #64

Closed
valotas opened this issue Jan 14, 2015 · 7 comments
Closed

date validation before setting it #64

valotas opened this issue Jan 14, 2015 · 7 comments

Comments

@valotas
Copy link
Contributor

valotas commented Jan 14, 2015

Using an input format like DD.MM.YYYY and setting the date to something like 12.30.1980 the calendar will compute it as 12.01.0000.

Is there a way to validate the given date before using it? I would expect something like moment(input, 'DD.MM.YYYY').isValid() before computing the thing.

@bevacqua
Copy link
Owner

Can you show me the code?

@valotas
Copy link
Contributor Author

valotas commented Jan 15, 2015

Sure, here you are: http://jsbin.com/siyecocoja

@mlent
Copy link
Contributor

mlent commented Jan 15, 2015

Also having this problem.

It looks like this happens when the input field starts blank, and then a user provides an invalid date. If you give the input field a valid initial date, it will "revert" to the previous date if a user tries to type something invalid.

@bevacqua
Copy link
Owner

Okay, so, technically what Rome is doing isn't wrong. As you type, Rome parses 12.3 as a valid date for 12.03.0000. From 12.30 on, the date is completely invalid so it falls back to the last known valid date, 12.03.0000. If you don't want dates in the year 0000 to be valid, you should set a min date, which will make the date invalid until the user enters a date that includes a valid year as well. Another option is to take the year first, that way it'll be more obvious and natural.

This is, however, not a bug.

@valotas
Copy link
Contributor Author

valotas commented Jan 15, 2015

Well, it is not a bug, but it would be better to give a hook to validate given inputs.

I did not find something like that. Setting a min date is a partial solution, as I do not always know the minimum date.

If you give me a hint on where to start, I could implement it.

@bevacqua
Copy link
Owner

The problem is that:

moment('12.3', 'DD.MM.YYYY').isValid()
// <- true

moment has a strict option, but using that would be a breaking change.

moment('12.3', 'DD.MM.YYYY', true).isValid()
// <- false

moment('12.30.1980', 'DD.MM.YYYY', true).isValid()
// <- false

moment('12.03.1980', 'DD.MM.YYYY', true).isValid()
// <- true

@bevacqua
Copy link
Owner

I would create an option for strict date validation, and then make it the default in a later version.

@valotas valotas mentioned this issue Jan 30, 2015
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

3 participants