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?

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108

2 Answers2

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    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? – Yair Nevet Jan 29 '13 at 08:52
  • 2
    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 `` 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. – Darin Dimitrov Jan 29 '13 at 09:46
  • 1
    How JavaScript can find the browser language setting? – Yair Nevet Jan 30 '13 at 11:32
  • 2
    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. – Darin Dimitrov Jan 30 '13 at 11:37
  • 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. – Lee Mar 23 '15 at 19:46
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.

Community
  • 1
  • 1
Dave Alperovich
  • 32,320
  • 8
  • 79
  • 101
  • Your references not answers why certain date formats are valid while others are not, what is the logic/convention behind it? – Yair Nevet Jan 28 '13 at 07:27
  • 1
    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 possibilities – Dave Alperovich Jan 28 '13 at 07:34