views:

343

answers:

2

How do I reset an asp.net validation control via JavaScript? The current code sample clears the error message text but does not reset the validation control for the next form submission.

var cv= document.getElementById("<%= MyValidationContorl.ClientID %>");
cv.innerHTML = '';

Update:

Here is the full code sample of the form. I can not seem to get the validation controls fire off on another form submission:

function ClearData() {
    var cv = document.getElementById("<%= MyValidationContorl.ClientID %>");
    cv.innerHTML = '';
}

<html>
   <form>
       <asp:TextBox id="MyTextControl" runat="server" />
       <asp:CustomValidator ID="MyValidationContorl" runat="server" />
       <input type="button" onclick="javascript:ClearCCData(); return false;" runat="server" />
   </form>
</html>
A: 

Here's code to reset all validators

function CleanForm() {
    document.forms[0].reset(); 

    for (i = 0; i < Page_Validators.length; i++) {
        Page_Validators[i].style.visibility = 'hidden';
    }

    return false;
}

or a single one:

document.getElementById("<%= MyValidationContorl.ClientID %>").style.visibility
 = 'hidden';
Glennular
This was not helpful.
Michael Kniskern
oops sorry miss read your goal
Glennular
It hides the validation controls, but when I try another form submission it does not fire off the validation controls again.
Michael Kniskern
+2  A: 

Page validation is fired every time you do a post, what appears to be the problem is that you are clearing the validator content cv.innerHTML = '';, this way your validator message is lost forever and you'll think validation is not firing again.

and for @Glennular answer, the code does not handle the validator Display property, if its set to Dynamic the validator will be toggled using validator.style.display, but if its set to None or Inline then validator.style.visibility property will be used instead.

Its better to use asp.net ValidatorUpdateDisplay instead,

<script type="text/javascript">
    function Page_ClientValidateReset() {
        if (typeof (Page_Validators) != "undefined") {
            for (var i = 0; i < Page_Validators.length; i++) {
                var validator = Page_Validators[i]; 
                validator.isvalid = true;
                ValidatorUpdateDisplay(validator);
            }
        }
    }
</script>
MK