views:

604

answers:

2

How can I validate that at least 1 radio button is selected?

+5  A: 

If all the radio buttons are part of the same RadioButtonList, you can use a simple RequiredFieldValidator. Here's an example of that:

<asp:RadioButtonList 
    ID="RadioButtonList1"
    runat="server"
    RepeatColumns="3">
    <asp:ListItem>Red</asp:ListItem>
    <asp:ListItem>Yellow</asp:ListItem>
    <asp:ListItem>Blue</asp:ListItem>
    <asp:ListItem>Green</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator 
    ID="ReqiredFieldValidator1"
    runat="server"
    ControlToValidate="RadioButtonList1"
    ErrorMessage="You must Select your favorite color!">*
</asp:RequiredFieldValidator>

If you have RadioButtons in different RadioButtonLists and you want to validate that at least one is selected across multiple lists, then you would need a CustomValidator.

Jose Basilio
@Blankman - Did this answer help you? If so, please accept it.
Jose Basilio
+2  A: 

Another way might be if you checked one of them by default, during pageload (maybe the one you think most users will select) then the user will be "forced" to change the selection or continue with the default option, in this way you make sure there is always an option selected.

Izabela
in same cases, you want force the user to make a choice. creating a default choice is not the same thing.
Homer