Sunday, April 26, 2009

Clear Validation Errors and Validation Summary messages

ASP.net built in validation does not provide us a straight forward to clear all the validation errors. This would be really helpful while resetting a form. The reset html button would simply reset the form values but will not clear the validation errors.

The following javascript code snippet can be used to clear the validation error messages.
Have a reset button on your form and call the following js function onclick.


<input type="reset" onclick="HideValidationErrors();" />



function HideValidators() {
//Hide all validation errors
if (window.Page_Validators)
for (var vI = 0; vI < Page_Validators.length; vI++) {
var vValidator = Page_Validators[vI];
vValidator.isvalid = true;
ValidatorUpdateDisplay(vValidator);
}
//Hide all validaiton summaries
if (typeof (Page_ValidationSummaries) != "undefined") { //hide the validation summaries
for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
summary = Page_ValidationSummaries[sums];
summary.style.display = "none";
}
}
}

Digg It! Add to del.icio.us Stumble This

2 Comments:

Anonymous said...

Thanks for the code! I works great to hide previous error messages otherwise left showing when my popup window is reshown.

FishOfPrey said...

Very helpful. Thanks.

Note that the onclick function HideValidationErrors doesn't match the sample function HideValidators in your samples. Still, I get the idea.