Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC DateTime validation - UK Date format

I have a simple view with two date fields with ValidationMessageFor controls added for the unobtrusive JavaScript validation.

My issue is I keep getting told my date is invalid, when it is in correct format (dd/MM/yyyy)

I have added <globalization culture="en-GB" uiCulture="en-GB"/> to my web.config, and also included [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] on each DateTime property, yet it still won't accept UK format dates.

Is there anything obvious I am missing here?

like image 983
mp3duck Avatar asked Aug 21 '12 10:08

mp3duck


1 Answers

Actually you just need to overload unobtrusive JavaScript validation method for date

jQuery(function ($) {     $.validator.addMethod('date',     function (value, element) {         if (this.optional(element)) {             return true;         }          var ok = true;         try {             $.datepicker.parseDate('dd/mm/yy', value);         }         catch (err) {             ok = false;         }         return ok;     }); }); 
like image 69
Bohdan Avatar answered Oct 11 '22 10:10

Bohdan