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.