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?
jQuery is a Javascript library. An unobtrusive validation in jQuery is a set of ASP.Net MVC HTML helper extensions.By using jQuery Validation data attributes along with HTML 5 data attributes, you can perform validation to the client-side.
In MVC, validation happens on both the client and server. Fortunately, .NET has abstracted validation into validation attributes. These attributes contain validation code, thereby reducing the amount of code you must write. View or download sample from GitHub.
DevExpress MVC data editors support an unobtrusive client validation approach which is implemented in ASP.NET MVC 3 and higher. This approach implies decorating model class properties with the DataAnnotations attributes and jQuery Validation.
The default value of MvcOptions.MaxValidationDepth is 32. Validation is automatically short-circuited (skipped) if the model graph doesn't require validation.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With