22

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>
1
  • @azamsharp -- I mean to enable the start the validation process from the beginning. Commented May 26, 2010 at 18:59

5 Answers 5

41

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @MK .. your function works great .. It resets all the validators as I wanted but it doesn't reset the validation summary. Do you have any code that might do that job? Thanks a lot. I am desperately needing it.
@LaurenceNyein I updated the answer to include a reset validation summaries method.
17

This one resets all validators in all validation groups.

<script type="text/javascript">
    Page_ClientValidate('');
</script>

5 Comments

If you use this, make sure you have a ValidationGroup defined for all validators... otherwise it will trigger validators without a group. If you're really lazy and don't want to define a validation group for every element, you could make up a random string that won't be used as a validation group like so: Page_ClientValidate('reset-all');
Empty string would fire all validators.
That does not reset the validators. That fires validation against an empty ValidationGroup, which could well be present, as @dhsto says.
There is a difference between Page_ClientValidate() and Page_ClientValidate('')
It would be even better if you pass a string that can never be a name of a validation group (e.g. a GUID). This will cause ASP.NET to perform a validation on a non-existing validation group and thus reset all validators.
0

Try the following chunk of code :

$("#<%= txtUserSettingsEmailRequiredValidator.ClientID %>").css("display", "none");

I hope this will work as it worked for me. :)

Comments

-1

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';

1 Comment

It hides the validation controls, but when I try another form submission it does not fire off the validation controls again.
-3

Using the Page_Validators[i].style.visibility = 'hidden'; Don't work for me so I use this line of code instead: Page_Validators[i].enabled = false;

if (sFirstName == "" && sLastName == "") 
    {

        alert('Reminder: Please first enter student ID to search for the student information before filling out the rest of the form field values');
        //Disable all require field validation coontrol on the form so the user could continue to use the Lookup student function.
        document.forms[0].reset();
        for (i = 0; i < Page_Validators.length; i++) {
            //Page_Validators[i].style.visibility = 'hidden';
            Page_Validators[i].enabled = false;
        }
        return false;
    }
    else 
    {
        alert('Student Name = ' + sFirstName + ' ' + sLastName);
        document.forms[0].reset();

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.