有没有办法用标注完全重置asp.net验证器?

时间:2013-09-25 19:26:20

标签: javascript asp.net validation webforms

我有一个网络表单,我希望用户能够在不完整回发的情况下重置表单(我知道这是最简单的方法)。

我正在使用带有标注的asp.net验证器,没什么太花哨的。

<asp:TextBox ID="txtWarranty" MaxLength="50" runat="server" />
<asp:RequiredFieldValidator ID="rfvWarrantyID" runat="server" Display="None"
    ControlToValidate="txtWarranty" ErrorMessage="Warranty ID is required"></asp:RequiredFieldValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="vceWarrantyID" HighlightCssClass="wpcf7-not-valid" TargetControlID="rfvWarrantyID"
    runat="server" />

当用户重置表单时,我想清除所有字段并重置所有验证器。它大部分都有效。

function ResetWarrantyForm() {
    document.forms[0].reset();
    ClearValidation();
}
function ClearValidation() {
    var validators = window.Page_Validators;
    for (var i = 0; i < validators.length; i++) {
        validators[i].IsValid = true;
        window.ValidatorUpdateDisplay(validators[i]);
        try {
            var callOutId = validators[i].ValidatorCalloutBehavior._id;
            var callOut = window.$find(callOutId);
            if (callOut)
                callOut.hide();
        }
        catch (e) { }
        validators[i].IsValid = true;
        window.ValidatorUpdateDisplay(validators[i]);
    }
    if (typeof (window.Page_ValidationSummaries) != "undefined") { //hide the validation summaries  
        for (var sums = 0; sums < window.Page_ValidationSummaries.length; sums++) {
            var summary = window.Page_ValidationSummaries[sums];
            summary.style.display = "none";
        }
    }
    $("input[type='text'], select").removeClass("wpcf7-not-valid").removeClass("comboboxNotValid");
}

问题是一旦评估了验证。输入设置为在焦点上进行验证。因此,当重置表单时,用户单击文本框。他们立即被告知他们的输入无效,即使他们还没有做任何事情。

在第一页加载时,字段在单击按钮之前不会生效。但是在第一个按钮之后,单击焦点和模糊上的字段验证。

有没有办法重置这种行为,好像它是第一次加载?

2 个答案:

答案 0 :(得分:0)

您可以使用javascript重置验证器。

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

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

    return false;
}

答案 1 :(得分:0)

我最后添加了这个javascript函数

function ClearValidation() {
    //reset the form
    document.forms[0].reset();
    //get all the validators
    var validators = window.Page_Validators;
    for (var i = 0; i < validators.length; i++) {
        validators[i].IsValid = true;
        window.ValidatorUpdateDisplay(validators[i]);
        //Clear out all the callouts and hide them
        try {
            var callOutId = validators[i].ValidatorCalloutBehavior._id;
            var callOut = window.$find(callOutId);
            if (callOut)
                callOut.hide();
        }
        catch (e) { }
        // I can't remember why I repeated this code, not sure if necessary
        validators[i].IsValid = true;
        window.ValidatorUpdateDisplay(validators[i]);
    }
    // hide the summary
    if (typeof (window.Page_ValidationSummaries) != "undefined") { //hide the validation summaries  
        for (var sums = 0; sums < window.Page_ValidationSummaries.length; sums++) {
            var summary = window.Page_ValidationSummaries[sums];
            summary.style.display = "none";
        }
    }
    // remove the error class that I set in the callout server control
    $("input[type='text']").removeClass("wpcf7-not-valid");
    $("select").removeClass("wpcf7-not-valid");
}