Validation error when choosing a date greater than 12

In my mvc4 application, if I select a date greater than 12, I get a "Date is invalid" error. It works well for a date less than 12 .. The format obtained when selecting a date in datepicker is "mm / dd / yyyy". My model: -

[Required(ErrorMessage = "Date Of Birth is required")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] [Display(Name = "Date Of Birth")] public DateTime? DOB{ get; set; } 

Script for datepicker: -

 <script type="text/javascript"> $(document).ready(function () { debugger; $('.datepicker').each(function () { $(this).removeClass('hasDatepicker').datepicker(); $(this).datepicker(); }); }); </script> 

Please tell me the solution for this. Thanks..

+4
source share
1 answer

I do not believe that there is magic that would pass the date format from the annotations of your model data into jQueryUI. You must do this explicitly:

 $(this).datepicker({ dateFormat: "mm/dd/yyyy" }); 

Link this documentation .

0
source

All Articles