How DateTime is validated in MVC Unobtrusive Validation?
B

2

7

Using MVC4 with client-side & unobtrusive validation enabled, I'm trying to understand how the validation determine if an entered DateTime value is valid or not.

In my application this formatted date is valid: 01/31/2013, while if I enter: 31/01/2013, I get:

The field [fieldId] must be a date

How does it determined what is a valid date?

Belong answered 27/1, 2013 at 23:33 Comment(0)
B
4

How does it determined what is a valid date?

It uses the current culture defined on your client browser to determine the correct format. For example if your browser is configured to use en-US as default language then the correct format is M/d/yyyy. If you client browser is configured to use fr-FR language the correct format is dd/MM/yyyy.

It will all depend on what language is your browser configured to use.

Broeker answered 29/1, 2013 at 7:8 Comment(5)
Darin, thank you! It's interesting, the client request data(languages etc.) is sent to the client-side validation scripts? otherwise, how JS can find out this information to work with?Belong
Unobtrusive client side validation runs on the client. It uses javascript and it uses the browser language setting to parse the date. The server side validation will use either the client side language (if you specified culture="Auto" and uiCulture="Auto" in your <globalization> element) or it will use a predefined server side culture. The problem comes when the 2 are different. Your client side validation may succeed while your server side validation fail or vice versa.Broeker
How JavaScript can find the browser language setting?Belong
JavaScript runs in the browser. So when you for example attempt to parse a date using its constructor the native call will use the browser language to perform this parsing.Broeker
This drove me crazy for a while. I didn't know why my client-side validation kept invalidating when it shouldn't. But I knew it validated server-side. In the end I disabled obtrusive js and client side validation under MVC web config. Thanks for the explanation.Essentialism
B
2

Great Question. This seems to be an ongoing issue from MVC 3. I see it all the time. There are countless complaints about the issue like this http://forums.asp.net/t/1831712.aspx/1

Darin Dimitrov wrote a great post in which he uses DataStringFormat to force it.

Format datetime in asp.net mvc 4

I highly recommend it. Great read. If you use it, give a +1 to his answer.

For my $$$ I will stick to and swear by JQuery date pickers populating uneditable controls for the user's benefit.

Ballard answered 27/1, 2013 at 23:46 Comment(2)
Your references not answers why certain date formats are valid while others are not, what is the logic/convention behind it?Belong
Yair, I don't have a perfect answer for you. MVC Unobtrusive Validation works with varying kinds of Regex. Dates can format in so many variations, I suspect it's very hard to have a valid regex for every possibility. That seems to be in the implication of Darins post too. He forces a single format. I bypass the whole game and give the user a JQuery date picker. That way I don't have to worry about accounting for all the possibilitiesBallard

© 2022 - 2024 — McMap. All rights reserved.