validation: Ensuring a Radio Button has been selected

I have a page where a user MUST select a Radio Button.

When the page is first displayed there is no Radio Button selected.

I thought I could use the Custom Validation Control to make a check like this: This checks if either Radio button is selected and then returns IsValid as False if neither is selected.

In Script:

    Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal e As System.Web.UI.WebControls.ServerValidateEventArgs)
        If MedicalTrue.Checked = False Then
            If MedicalFalse.Checked = False Then
                e.IsValid = False
            End If
        End If
    End Sub

In Body:

                    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please indicate if you have a Pre-existing Medical Condition" ControlToValidate="MedicalTrue" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
                              <p>Do you have a Pre-existing medical condition?
                        <asp:RadioButton ID="MedicalTrue" runat="server" GroupName="medical" Text="Yes" />
                        <asp:RadioButton ID="MedicalFalse" runat="server" GroupName="medical" Text="No" /></p>

However, I keep getting the following error:

Control 'MedicalTrue' referenced by the ControlToValidate property of 'CustomValidator1' cannot be validated.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


You can simply use RequiredFieldValidator to validate the RadioButtonList or RadioButton. Just give ControlToValidator to your RadioButton's ID.

gilkesyAuthor Commented:
I'm not sure that would work becuase I'm validating the two radio buttons as a group.

If I place a RequiredFieldValidator control on the "MedicalTrue" radio button then it will require that the "MedicalTrue" radiobutton is selected.

Anyway, I still get the error message:

Control 'MedicalTrue' referenced by the ControlToValidate property of 'RequiredFieldValidator1' cannot be validated.

Instead of using two radio buttons you can use radiobuttonlist and reference its id to ControlToValidate.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
how to validate the radio button in, i want the solution either in javascript or in c#
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.