Required field validator for multiple checkbox

Last post 07-04-2009 10:51 AM by deepthoughts. 3 replies.

Sort Posts:

  • Required field validator for multiple checkbox

    07-03-2009, 10:27 AM
    • Member
      1 point Member
    • saurabh.15in
    • Member since 10-01-2007, 5:52 AM
    • Posts 32

     Hi,

    How can i validate multiple check box or checkboxlist using required field validator .

    i want to check if any one check box is checked or not.

     

    Thanks,

    Saurabh

  • Re: Required field validator for multiple checkbox

    07-03-2009, 11:23 PM
    • Contributor
      4,166 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 9:55 AM
    • Posts 615

    Although RequiredFieldValidators can be applied on RadioButtonLists and DropDownLists, you can't apply them on CheckBox or CheckBoxLists. Because its valid to have a checkbox not checked at all.

    So in order to put this kind of logic, you'll have to go for CustomValidators. Please note that we'll keep the ControlToValidate property of the CustomValidator to empty. Because a validator can't be applied on a CheckBoxList.

    Lets suppose we've a checkbox list of three checkboxes and we want to do postback only if any one of three is checked.

     

        <asp:CheckBoxList ID="chkList1" runat="server">
        <asp:ListItem>Red</asp:ListItem>
        <asp:ListItem>Green</asp:ListItem>
        <asp:ListItem>Blue</asp:ListItem>
        </asp:CheckBoxList>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="CheckBoxCheck" ErrorMessage="Please check at least one" ValidationGroup="VG"></asp:CustomValidator>
        <asp:Button ID="btn1" runat="server" Text="Submit" ValidationGroup="VG" />


     

    And the javascript method which does the validation is

     

            function CheckBoxCheck(sender, args) {
                if (document.getElementById("chkList1_0").checked || document.getElementById("chkList1_1").checked || document.getElementById("chkList1_2").checked) {
                    args.IsValid = true;
                    return;
                }
                args.IsValid = false;
            }


     

    Please refer to following thread for more details

     

    http://forums.asp.net/t/1218279.aspx

     

    Hope it helps

    MARK AS ANSWER if it helps
  • Re: Required field validator for multiple checkbox

    07-04-2009, 10:33 AM
    • Member
      1 point Member
    • saurabh.15in
    • Member since 10-01-2007, 5:52 AM
    • Posts 32

    Thanks a lot...It not only soved my problem but i got idea how to solve some other issues also....

  • Re: Required field validator for multiple checkbox

    07-04-2009, 10:51 AM
    Answer
    • Contributor
      4,166 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 9:55 AM
    • Posts 615

    If it solved your problem then please mark it as answer.

    All the best

    MARK AS ANSWER if it helps
Page 1 of 1 (4 items)