MVC3 Validation with ComponentModel.DataAnnotations for UK date format (also using jquery ui datepicker)

前端 未结 6 875
后悔当初
后悔当初 2021-01-05 14:25

I see there are some similar questions to this, but none solve my issue.

I am working on an MVC3 app with Entity Framework 4.3. I have a UK date field that i plan t

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-05 15:02

    Actually I have found a better solution here.... by #fretje

    Override jquery date

    I have modified his/her code slightly though so that it can still take date formats like 30 May 2012 below.

    $(function () {
        $.validator.addMethod(
            "date",
            function (value, element) {
    
                //Added to validate dates like 31 May 2012
                if (value.split('/').length === 1) 
                    return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
    
                var bits = value.match(/([0-9]+)/gi), str;
                if (!bits)
                    return this.optional(element) || false;
                str = bits[1] + '/' + bits[0] + '/' + bits[2];
                return this.optional(element) || !/Invalid|NaN/.test(new Date(str));
            },
            "Please enter a date in the format dd/mm/yyyy"
        );
    
        $global.init();
    });
    

提交回复
热议问题