Press enter to see results or esc to cancel.

How To Validate A CheckBox ASP.NET Control

If you attempt to use a RequiredFieldValidator with a CheckBox control, you will get a runtime error that says something like “Control {your checkbox control id} referenced by the ControlToValidate property of {id of your validator control} cannot be validated”.

If you want to require an ASP.NET CheckBox control be checked when a form is submitted, what you need to do instead is use a CustomValidator control with a ClientValidationFunction set as follows:

<asp:CheckBox runat="server" ID="CheckBox1" />
<asp:CustomValidator ID="CustomValidator1"  runat="server" ClientValidationFunction="CheckBox1_ClientValidate"></asp:CustomValidator>

Your client validation javascript function would look something like this then:

function CheckBox1_ClientValidate(sender, args) {
	args.IsValid = document.getElementById('<%=CheckBox1.ClientID%>').checked;
}

As always, don’t forget to also set the ValidationGroup property of both your validator and input controls.

Comments

Leave a Comment