具有ValidationSummary的复选框自定义验证器不起作用

时间:2011-01-25 10:51:54

标签: asp.net checkbox customvalidator

我有以下验证,这对我的其他字段工作正常,但是尝试让自定义验证器作为复选框的验证摘要的一部分工作但没有快乐。

这就是我现在所拥有的

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

但是,当我点击提交时,我只看到其他字段的错误消息,而且此复选框没有任何内容......任何想法?

1 个答案:

答案 0 :(得分:2)

你的代码中有这个:

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

将其更改为:

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

同时为ControlToValidate设置CustomValidator属性:

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