Modify the default authentication method in the jquery validation _jquery

Source: Internet
Author: User
In a recent project, using the jquery validation validation date, the problem encountered and an unexpected situation is that in ASP.net MVC 3 project, for <input type= "true"/ > element, if you call the form's valid method to validate the form, although I do not add a date validation setting, just type= "date", but it still calls the date validation logic to verify that the date format is correct. This would have been a good behavior, but the problem is that its supported date format is limited, and the code to read jquery validation will know (of course, the documentation is also available), and for "date," date validation is only validated using JavaScript-built processing, reflected in the code, is to see whether the new Date (date string) succeeds. In My computer, enter the date "9/5/2012" to validate the pass, but "2012-9-5" validation failed, which is clearly the correct format. I need to change or improve this behavior. How to do it?

Of course, we can modify the jquery validation source code, but first of all, I am referring to the CDN code, and secondly, as a public library, I would like to try not to change, last, maybe we have forgotten to modify it, and when the official update of the new version, We get down to the update and the result is to cover our own changes. A better approach would be to play patches and replace the date validation functions from the outside with our own. Here, perhaps, to thank JavaScript Oo is not so thorough, and did not put those built-in validation methods into Protected/private, read the source code, the replacement method is very simple, after referencing the jquery validation source, and then replace:

Copy Code code as follows:

$.validator.methods.date = function (value, Element)
{
The Date.parse function originates from another library that processes dates
return this.optional (Element) | | Date.parse (value)!= null;
};

In addition to replacing the default message, you can use the same method, such as:
Copy Code code as follows:

$.validator.messages.date = "Hey, entered an invalid date"

Of course, there are other better ways to handle messages, which are mentioned in the description of the globalization and localization of jquery validation.

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.