How to Validate A CheckBox in ASP.Net 3.5 / Checkbox Validation Control Sample Code in ASP.Net, C# (C Sharp) And Visual Basic
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.
- 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)
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 !
- And then paste the below code into the body section of your page;
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 :)