通过 JavaScript 重置 ASP.NET 验证控件?

发布于 2024-09-03 02:11:36 字数 717 浏览 3 评论 0原文

如何通过 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里的微风 2024-09-10 02:11:36

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

对于@Glennular答案,代码不处理验证器 Display 属性,如果将其设置为Dynamic,则将使用 validator.style.display< 切换验证器/code>,但如果将其设置为 NoneInline 则将使用 validator.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>

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>
如日中天 2024-09-10 02:11:36

此操作会重置所有验证组中的所有验证器。

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

This one resets all validators in all validation groups.

<script type="text/javascript">
    Page_ClientValidate('');
</script>
雾里花 2024-09-10 02:11:36

尝试以下代码块:

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

我希望这对我有用。 :)

Try the following chunk of code :

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

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

娇妻 2024-09-10 02:11:36

以下是重置所有验证器

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

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';
烟酉 2024-09-10 02:11:36

使用Page_Validators[i].style.visibility = 'hidden';
不适合我,所以我使用这行代码: 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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文