复选框必需的验证器

时间:2014-01-02 14:57:05

标签: c# javascript asp.net validation checkbox

我在asp.net中开发了一个应用程序,在我的页面上有复选框,这是必填字段,我在验证后编码,但按下保存验证时没有调用。

<asp:CheckBox ID="ChkContent" runat="server" Text=" I hereby certify that the information above is true and accurate."
                        Font-Bold="true" />
                    <asp:CustomValidator ID="Custom1"  CssClass="errorTop" ClientValidationFunction="CheckBoxValidation" runat="server"
            ErrorMessage="You must select this box to proceed" ></asp:CustomValidator> 

,保存按钮代码为

   <asp:Button ID="BtnSaveMiscDetail" ValidationGroup="AddMisc" CssClass="buttonGrid"
                            runat="server" Text="Save" Width="60px" OnClick="BtnSaveMiscDetail_Click" />

客户端验证码

function CheckBoxValidation(oSrouce, args) {
        var myCheckBox = document.getElementById("ContentPlaceHolder1_MiscDetails1_ChkContent");

        if (myCheckBox.checked) {

            args.IsValid = true;

        }
        else
            args.IsValid = false;

    }

验证功能没有调用。

1 个答案:

答案 0 :(得分:0)

在自定义验证程序控件中添加ValidationGroup="AddMisc"

<asp:CheckBox ID="ChkContent" runat="server" Text=" I hereby certify that the information above is true and accurate."
                        Font-Bold="true" />
                    <asp:CustomValidator ID="Custom1" 
                          CssClass="errorTop" 
                          ClientValidationFunction="CheckBoxValidation" 
                          runat="server"
                          ErrorMessage="You must select this box to proceed"
                          ValidationGroup="AddMisc">
                    </asp:CustomValidator>