Monday, December 5, 2011

How To Validate CheckBox within MasterPage Through CustomValidator Control?

Step 1: Add CustomValidator control, set its ClientValidationFunction property and set an ErrorMessage.

<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="fnCheckAgree"
ErrorMessage="You Must Choose This CheckBox" Display="Dynamic"></asp:CustomValidator>


Step 2: Add JavaScript Code as follows,

<script>
function fnCheckAgree(source,arguments)
{
if (document.getElementById('<%=CheckBox1.ClientID%>').checked==true)
arguments.IsValid=true;
else
arguments.IsValid=false;
}
</script>


NOTE: The above JavaScript code contains an inline block of Asp.Net code to retrieve the ClientID of the CheckBox control.

No comments:

Post a Comment