Learning While Aging

Clear ASP.NET validation errors on client side with JavaScript

asp-net

[UPDATE] the function in this post is for .NET 1.1 web applications only (maybe .NET 2.0). And it is not recommended to use this function in latest .NET framework.

I got an unusual request today that asks to clear out all ASP.NET (.NET 1.1) validation error messages when a “Clear” button is clicked. Also, it is requested to be done on the client side to avoid PostBack, so I cannot just reload the page to clear out the error messages and I have to use JavaScript. Instead of writing everything from scratch, I used the built-in ASP.net client side JavaScript library and created a JavaScript function called “clearValidationErrors()” as follows:

function clearValidationErrors()
{
    var i;
    for (i = 0; i < Page_Validators.length; i++)
    {
        Page_Validators[i].style.display = "none";
    }
}

Then I added an HTML button on the page:

<input type="button" value="Clear Error Messages" name="Clear" onclick="clearValidationErrors();"/>

This function only clears errors generated by the built-in ASP.NET validators, so if use your own JavaScript validator, then it will not work for you.