21

如何通过 JavaScript 重置 asp.net 验证控件?当前代码示例会清除错误消息文本,但不会为下一次表单提交重置验证控件。

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

更新:

这是表单的完整代码示例。我似乎无法在另一个表单提交时触发验证控件:

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>
4

5 回答 5

39

每次发布帖子时都会触发页面验证,问题似乎是您正在清除验证器内容cv.innerHTML = '';,这样您的验证器消息就会永远丢失,您会认为验证不会再次触发。

对于@Glennular 的回答,代码不处理验证器Display属性,如果它设置为Dynamic验证器将使用 切换validator.style.display,但如果其设置为Noneor Inlinethenvalidator.style.visibility属性将被使用。

最好改用asp.net ValidatorUpdateDisplay

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

更新:重置验证摘要

<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>
于 2010-06-01T13:07:39.993 回答
17

这个重置所有验证组中的所有验证器。

<script type="text/javascript">
    Page_ClientValidate('');
</script>
于 2012-02-16T17:44:20.843 回答
0

尝试以下代码块:

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

我希望这对我有用。:)

于 2011-10-26T08:54:38.737 回答
-1

这是重置所有验证器的代码

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

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

    return false;
}

或单个:

document.getElementById("<%= MyValidationContorl.ClientID %>").style.visibility
 = 'hidden';
于 2010-05-26T18:58:52.783 回答
-3

使用Page_Validators[i].style.visibility = 'hidden'; Don't work for me 所以我改用这行代码: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;
    }
于 2013-04-03T22:14:13.017 回答