Where trigger the “Please enter a valid date.” in jquery.validate.js

北城以北 提交于 2020-01-25 21:32:06

问题


I am using jQuery UI version 1.12.1 and jQuery Validation v1.15.0

I created a datepicker as follow:

if ($.fn.datepicker) $(':input.date').datepicker({
          changeMonth: true,
          changeYear: true
    });

By default, the date format will be mm/dd/yyyy, for example 01/26/2017, represent 26th January 2017.

I am trying to change the date format to be dd/mm/yyyy, so I change the datepicker as follow:

if ($.fn.datepicker) $(':input.date').datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: 'dd/mm/yy'
});

Well, now the date format display correct as I wish, it become 26/01/2017, represent 26th January 2017.

However, it throw a red error message Please enter a valid date.. After my debugging, I found that the error message is actually come from the jquery.validate.js at line 357:

messages: {
        // some other code
        date: "Please enter a valid date.",
        // some other code
    },

Any idea how/why it throw this error message?

Is it my jQuery UI version not compatible with the jQuery validation version?

kindly advise.


回答1:


Found it, it was actually trigger by a date method validation in jquery.validate.min.js, which is in line 1356:

// http://jqueryvalidation.org/date-method/
        date: function( value, element ) {
            return this.optional( element ) || !/Invalid|NaN/.test( new Date( value ).toString() );
        },


来源:https://stackoverflow.com/questions/41435873/where-trigger-the-please-enter-a-valid-date-in-jquery-validate-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!