Unobtrusive DateTime? Validation in MVC4

前端 未结 5 1947
情深已故
情深已故 2021-02-08 08:21

I upgraded an MVC3 solution to MVC4. After the migration, the validator is broken.

My input date, if i select German as language, is \"20.03.2013\". I get an validation

5条回答
  •  滥情空心
    2021-02-08 08:47

    Try overwriting the default date validator:

    // Replace dots so jq validator can understand what's going on
    $(function () {
        $.validator.addMethod(
        "date",
        function (value, element) {
            var s = value;
            s = value.replace(/\./g, '/');
    
            // Chrome requires tolocaledatestring conversion, otherwise just use the slashed format
            var d = new Date();
            return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value))) || !/Invalid|NaN/.test(new Date(s));
        },
        ""
        );
    });
    
    $.validator.unobtrusive.parse();
    

提交回复
热议问题