Modify the default verification method in jQuery Validation

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.