How to validate asp:CheckBox?

Posted by Poster under ASP.NET category on | Views : 4166
To validate asp:Checkbox, you can't use RequiredFieldValidator. If you try to use RequiredFieldValidator, it will throw following error.

Control 'chkAgreed' referenced by the ControlToValidate property of 'ReqFieldValidatorId' cannot be validated.

So you need to use CustomValidator. Following is the code snippet


<asp:CheckBox ID="chkAgreed" runat="Server" Text="I agree." ForeColor="green" Font-Bold="true" />
<asp:CustomValidator ClientValidationFunction="ValidateCheckBox" runat="server" ID="cvf" />

JavaScript Code
<script language="javascript" type="text/javascript">
function ValidateCheckBox(source, args)
if(document.all["<%= chkAgreed.ClientID %>"].checked == false)
alert("Please check the checkbox to proceed.");
args.IsValid = false;


Comments or Responses

Login to post response