Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to Validate checkBox using Asp.net
Posted

 
Share this answer
 
Hi,
javascript function for client side validation (using jQuery).
JavaScript
function CheckBoxRequired_ClientValidate(sender, e)
{
    e.IsValid = jQuery(".AcceptedAgreement input:checkbox").is(':checked');
}

Code-behind for server side validation...
C#
protected void CheckBoxRequired_ServerValidate(object sender, ServerValidateEventArgs e)
{
    e.IsValid = MyCheckBox.Checked;
}

ASP.Net code for the checkbox & validator..
XML
<asp:CheckBox runat="server" ID="MyCheckBox" CssClass="AcceptedAgreement" />
<asp:CustomValidator runat="server" ID="CheckBoxRequired" EnableClientScript="true"
    OnServerValidate="CheckBoxRequired_ServerValidate"
    ClientValidationFunction="CheckBoxRequired_ClientValidate">You must select this box to proceed.</asp:CustomValidator>

And finally, in your postback - whether from a button or whatever...
C#
if (Page.IsValid)
{
    // your code here...
}

I hope it'll help you.

--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900