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>
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>
Update : Reset Validation Summaries
<script type="text/javascript">
function Page_ValidationSummariesReset(){
if (typeof(Page_ValidationSummaries) == "undefined")
return;
for (var i = 0; i < Page_ValidationSummaries.length; i++)
Page_ValidationSummaries[i].style.display = "none";
}
</script>
This one resets all validators in all validation groups.
<script type="text/javascript">
Page_ClientValidate('');
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With