Reset an asp.net validation control via javascript?

微笑、不失礼 提交于 2019-12-03 09:28:18

问题


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:


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>



回答2:


This one resets all validators in all validation groups.

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



回答3:


Try the following chunk of code :

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

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




回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/2915830/reset-an-asp-net-validation-control-via-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!