【问题标题】:Checkbox Custom Validator with ValidationSummary not working带有 ValidationSummary 的复选框自定义验证器不起作用
【发布时间】:2011-01-25 19:01:28
【问题描述】:

我有以下验证,在我的其他字段中运行良好,但试图让自定义验证器作为复选框验证摘要的一部分但没有乐趣。

这是我目前拥有的

<script language="javascript" type="text/javascript">
function ValidateTandCs(source, args)
{
    args.IsValid = document.getElementById('<%= optIn.ClientID %>').checked;
}
</script>

<asp:ValidationSummary CssClass="highlight"
    id="ValidationSummary1" 
     HeaderText="<p>Please amend these errors below to continue with your 
         application.</p>" Runat="server" />

<asp:CheckBox id="optIn" runat="server"></asp:CheckBox> I agree to the terms and 
               conditions of this site and I wish to Opt In for registration.
<asp:CustomValidator ID="valTandCs" ClientValidationFunction="ValidateTandCs" 
     ValidationGroup="ValidationSummary1" runat="server" 
     ErrorMessage="Please accept Terms and Conditions before submitting.">
</asp:CustomValidator>

但是当我点击提交时,我只看到我的其他字段的错误消息,而这个复选框什么也没看到...有什么想法吗?

【问题讨论】:

    标签: asp.net checkbox customvalidator


    【解决方案1】:

    你的代码中有这个:

     document.getElementById('<%= optIn.ClientID %><%= optIn.ClientID %>').checked;
    

    改成:

     document.getElementById('<%= optIn.ClientID %>').checked;
    

    还为CustomValidator 设置ControlToValidate 属性:

    <asp:CustomValidator ID="valTandCs" ClientValidationFunction="ValidateTandCs" 
     ControlToValidate="optIn" //
     ValidationGroup="ValidationSummary1" runat="server" 
    

    【讨论】:

    • 我实际上可以在复选框上添加一个 controltovalidate 我收到此错误 ControlToValidate 'valTandCs' 的属性引用的控件'optIn'无法验证。
    • 我更改了这一行 document.getElementById('').checked;但它仍然没有出现在验证摘要中,即使没有勾选,页面仍然可以验证。有什么想法吗?
    猜你喜欢
    • 2016-01-13
    • 1970-01-01
    • 2015-04-17
    • 2018-11-23
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    相关资源
    最近更新 更多