2

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

这就是我目前所拥有的

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

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

4

1 回答 1

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" 
于 2011-01-25T11:01:28.443 回答