Jquery Datepicker Chrome

前端 未结 6 1252
清歌不尽
清歌不尽 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:42

    I have had the same problem and is related with all Webkit based web browsers. If you set uppercase M the textbox show the moth with letters. The best solution for me was to override the validate date function from jquery.validate.js

    Create jquery.validate.date.js and ensure it is loaded after jquery.validate.js

    Add the following code to jquery.validate.date.js

    $(function() {
        $.validator.methods.date = function (value, element) {
            if ($.browser.webkit) {
    
                //ES - Chrome does not use the locale when new Date objects instantiated:
                var d = new Date();
    
                return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
            }
            else {
    
                return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
            }
        };
    });
    

提交回复
热议问题