How do you validate checkbox/radiobutton lists? will it be like normal validation?

Member Avatar for Sanchit_Sahu

yes...it does work in the same manner

checkboxlist validation

<%@ Register TagPrefix="CustomValidators" Namespace="CustomValidators" 
                        Assembly="filename_of_DLL_file" %>
<HTML>
  <body>
    <form runat="server">
      <asp:CheckBoxList id="CBList" runat="server">
        <asp:ListItem Value="Hello" />
        <asp:ListItem Value="World!" />
      </asp:CheckBoxList>

      <CustomValidators:RequiredFieldValidatorForCheckBoxLists 
              id="reqCBList" runat="server" ControlToValidate="CBList" 
              ErrorMessage="Please select at least one checkbox..." />

      <asp:Button id="Button1"  runat="server" Text="Button" />      
    </form>
  </body>
</HTML>

checkboxes validation

<form id="form1" runat="server">
    <asp:Label ID="NameLbl" runat="server" Text="Label"></asp:Label><asp:RequiredFieldValidator
        ID="NameRFV" runat="server" ErrorMessage="NameTxtBox"></asp:RequiredFieldValidator>
    <asp:TextBox ID="NameTxtBox" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="AliveLbl" runat="server" Text="Are you alive?"></asp:Label>
    <asp:CheckBox ID="AliveChkBox" runat="server" />
    <asp:Button ID="Submit" class="btn btn-primary" runat="server" Text="Submit"  OnClick="Submit_Click" />
</form>

listboxes

<asp:ListBox ID="ListBox1" runat="server" SelectionMode = "Multiple">
<asp:ListBox ID="ListBox1" runat="server" SelectionMode = "Multiple">
    <asp:ListItem Text = "Apple" Value = "1"></asp:ListItem>
    <asp:ListItem Text = "Mango" Value = "2"></asp:ListItem>
    <asp:ListItem Text = "Orange" Value = "3"></asp:ListItem>
</asp:ListBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="*Required"
ClientValidationFunction = "ValidateListBox"></asp:CustomValidator>
<script type = "text/javascript">
function ValidateListBox(sender, args) {
    var options = document.getElementById("<%=ListBox1.ClientID%>").options;
    for (var i = 0; i < options.length; i++) {
        if (options[i].selected == true) {
            args.IsValid = true;
            return;
        }
    }
    args.IsValid = false;
}
</script>
<asp:Button ID="Button1" runat="server" Text="Button" />

hope this can help you...

radio buttons

public bool check_radiobutton(RadioButton radio1, RadioButton radio2)
        {
            //none of them aare selected
            if ((radio1.Checked) && (radio2.Checked))
            {
                return false;
            }
            else
            {
                MessageBox.Show("You forgot to select a radiobutton!");
            }
            return true;
        }

Thanks

if(RadioButtonList1.SelectedIndex > -1)
{
    code you want the program to execute if a radio button is selected
}
else
{
    output statement asking the user to selecct a radio button
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.