Jquery Datepicker Chrome

前端 未结 6 1222
清歌不尽
清歌不尽 2020-11-27 04:22

When using jQuery UI Datepicker, we encouter a problem when used in Google Chrome: when we enter a date with a day higher than 12, it does not accept it as a valid date, th

6条回答
  •  北海茫月
    2020-11-27 04:43

    You've got to override default 'en-US' date validation with 'en-GB' date validation.

    • 'en-US' => 'mm/dd/yy'
    • 'en-GB' => 'dd/mm/yy'

    Solution:

    add a "jquery.validate.date.js" file in your project and put the following code in it:

    //To Fix jQuery date format 'en-GB' validation problem in Chrome
    $(function () {
        $.validator.addMethod(
            "date",
            function (value, element) {
                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 date in valid format [dd/mm/yyyy]"
        );
    });
    

    and make sure it load after the 'jquery.validate.min.js':

    
    
    
    

提交回复
热议问题