User experience is key on the web. With that in mind, I went out looking for some good looking validation and tooltip JQuery plugins.
I found a single solution and decided that using the new MVC 2.0 JQuery Validation, I will try and integrate it so that it works seamlessly with the ASP.NET MVC 2.0 Custom Validation.
Validation Engine – Because Validation is a mess.
Kudos to Cedric Dugas, because I really enjoy using this library, it’s clean, easy to use and it’s extremely flexible.
The first step was to determine where the validation was taking place. Inside of jquery.validate.js, go to line number 574. This is where the error message is compiled and added to a list. We will inject our validation engine code in here.
formatAndAdd: function(element, rule) {
var message = this.defaultMessage(element, rule.method),
theregex = /\$?\{(\d+)\}/g;
if (typeof message == "function") {
message = message.call(this, rule.parameters, element);
} else if (theregex.test(message)) {
message = jQuery.format(message.replace(theregex, '{$1}'), rule.parameters);
}
$.validationEngine.buildPrompt(element, message, "error");
this.errorList.push({
message: "",
element: element
});
this.errorMap[element.name] = message;
this.submitted[element.name] = message;
},
Notice that on line 9, I added the buildPrompt from the validation engine. I pass it the element and the message that describes the error.
Step was to determine where the errors are removed if they have passed the validation. On line 259, there is a unhighlight, which removes the background highlighting from the element with the error.
We will inject our closePrompt code here.
unhighlight: function(element, errorClass, validClass) {
$.validationEngine.closePrompt(element);
$(element).removeClass(errorClass).addClass(validClass);
}
The final step is to initialize the validation engine.
on line 50, after the submit button was clicked, we will initialize our validation engine.
if (validator.settings.onsubmit) {
$(validator.currentForm).validationEngine();
Here is the sample code. To utilize this code, all you will need to do is get the latest version of the Validation Engine and replace the jquery.validation.js with this one
ValidationDemo.zip (391.38 kb)
jquery.validate.js (48.89 kb)