How to Validate A CheckBox in ASP.Net 3.5 / Checkbox Validation Control Sample Code in ASP.Net, C# (C Sharp) And Visual Basic

This article will give you an idea to validate a checkbox in ASP.Net 3.5 ! It is so easy to implement and so handy to use !
6 April 2010
2 minutes read

Related Posts

As you know, ASP.Net has bunch of useful validation controls and they realy make our project so easy in terms of validations. But ASP.Net doesn't have any control to validate a check box !

There are lots of way to validate a check box and here you will find out some of them.


  1. First create a new page called checkbox.aspx and then paste this code into your new page head section;

    <script runat="server" language="vbscript">

        Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)

            args.IsValid = (Privacy1.Checked = True)

        End Sub


        Sub ValidateCheckBox()

            If CheckBox1.Checked = False Then

                Label1.Visible = True


                Label1.Visible = False

                'here is your function that you want to run after the checkbox is checked !

            End If

        End Sub


  2. And then paste the below code into the body section of your page;


<asp:CheckBox ID="Privacy1" runat="server" />

<br /><br />

<asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="CustomValidator1_ServerValidate">Please Check The Checkbox First !</asp:CustomValidator>

<br /><br />

<asp:Button ID="Button1" runat="server" Text="Button" /></div>

<br /><br />


<asp:CheckBox ID="CheckBox1" runat="server" /><br /><br />

<asp:Label ID="Label1" runat="server" style="color: Red;" Visible="false" Text="Please Check The Checkbox First !" />

<br /><br />

<asp:Button ID="Button2" CausesValidation="false" OnClick="ValidateCheckBox" runat="server" Text="Button" />


That's it ! Now you have two kinds of CheckBox RequiredFieldValidator. When you run the page and click one of the buttons, you will see that the checkbox will have a Required Field Validator. We can do that via Custon Validator Control on ASP.Net 3.5 Toolbar on the first examples and on the second one we are able to that manually !

I hope It helps :)