In a recent project, when jQuery Validation is used to verify the date, the problem encountered and an unexpected situation is that in ASP. for <input type = "date" data-val = "true"/> elements in the. net mvc 3 project, if the valid method of form is called to verify form, although I didn't add the date verification settings, only type = "date", but it still calls the date verification logic to verify that the date format is correct. This is a good behavior, but the problem is that the supported date formats are limited. Read the jQuery Validation Code (of course, as described in this document). For "date ", date verification only uses the built-in Javascript processing to verify the result. It is reflected in the Code to check whether the new Date (Date string) is successful. In my computer, the input date "" can be verified, but the "" Verification failed, the latter is obviously a correct format. I need to change or improve this behavior. How to do it?
Of course, we can modify the source code of jQuery Validation. But first, I reference the code on CDN. Second, as a public library, I 'd like to try not to modify it myself, after a long time, we may have forgotten to modify the new version. When the new version is officially updated, we will update it and overwrite our own changes. A better solution is to patch and replace the date verification function with our own from the outside. Thanks to the fact that Javascript OO is not so thorough, and the built-in verification methods are not converted into protected/private. After reading the source code, the replacement method is very simple, after referencing the jQuery Validation source code, replace it with the following:
Copy codeThe Code is as follows: $. validator. methods. date = function (value, element)
{
// The Date. parse function is derived from another database that processes the Date.
Return this. optional (element) | Date. parse (value )! = Null;
};
In addition, you can use the same method to replace the default message, for example:Copy codeThe Code is as follows: $. validator. messages. date = "hey, you entered an invalid date"
Of course, there are other better methods to deal with messages, which are mentioned in jQuery Validation's description of globalization and localization.