Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change date format validation, mvc 3 and jQuery

I been struggeling with this problem for two days now and have still not found the solution... I have added a datepicker to my website using this tutorial: http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding-a-jquery-validate-date-range-validator.aspx

Everything works except for the validation. I keep getting the error "Please enter a valid date". I have changed everything in my solution to "dd-MM-yyyy" and added globalization culture="da-DK" uiCulture="da-DK"

to my web.config. I still dosent work. Keep getting the error. Should I maybe changemy model class? The date I want to validate:

    [DataType(DataType.Date)]
    public DateTime DateOfBooking { get; set; }

Tried with:

    $('#formatdate').change(function () {
        $('#datpicker').datepicker("option", "dateFormat", "dd-mm-yy");
    });

and:

$(document).ready(function () {
function getDateYymmdd(value) {
    if (value == null)
        return null;
    return $.datepicker.parseDate("dd-mm-yy", value);
}
$('.date').each(function () {
    var minDate = getDateYymmdd($(this).data("val-rangedate-min"));
    var maxDate = getDateYymmdd($(this).data("val-rangedate-max"));
    $(this).datepicker({
        dateFormat: "dd-mm-yyyy",  // hard-coding uk date format, but could embed this     as an attribute server-side (based on the current culture)
        minDate: minDate,
        maxDate: maxDate
    });
});
});

Tried many different solutions but nothing seems to work. Cant figure out if the problem is in the mvc or jquery. Someone please help :) Thanks!

like image 217
Christian Avatar asked Oct 09 '22 18:10

Christian


1 Answers

It's the jquery.validate plugin which doesn't recognize this format. I would recommend you the following blog post. You could use the globalize plugin in order to set the preferred culture for client side validation.

like image 158
Darin Dimitrov Avatar answered Oct 13 '22 11:10

Darin Dimitrov