MVC日期时间验证 - 英国的日期格式

问题描述:

我添加了不显眼的JavaScript验证与ValidationMessageFor控制两个日期字段的简单视图。

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

我的问题是我不断收到告诉我的日期是无效的,当它在正确的格式(DD / MM / YYYY)

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

我已经加入<全球化文化=EN-GB的UICulture =EN-GB/> 来我的web.config,并且还包括 [DisplayFormat(DataFormatString ={0:DD / MM / YYYY},ApplyFormatInEditMode = TRUE)。每个日期时间属性,但它仍然不会接受英国格式化日期

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?

其实你只需要重载日期不显眼的JavaScript验证方法

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;
    });
});