Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How DateTime is validated in MVC Unobtrusive Validation?

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?

like image 333
Yair Nevet Avatar asked Jan 27 '13 23:01

Yair Nevet


People also ask

What is unobtrusive jQuery validation?

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.

How does validation work in MVC?

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.

What is client validation in DevExpress MVC data editor?

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.

What is the default value of mvcoptions maxvalidationdepth?

The default value of MvcOptions.MaxValidationDepth is 32. Validation is automatically short-circuited (skipped) if the model graph doesn't require validation.


2 Answers

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.

like image 137
Darin Dimitrov Avatar answered Oct 04 '22 21:10

Darin Dimitrov


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.

like image 28
Dave Alperovich Avatar answered Oct 04 '22 19:10

Dave Alperovich