CustomValidator – level 200

The CustomValidator control is really flexible. Here’s a quick example of validating that a checkbox is checked:

<%@ Page language=”c#” Codebehind=”ValidateCheckbox.aspx.cs” AutoEventWireup=”false” Inherits=”WebApplication1.ValidateCheckbox” %>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN” >
 function ValidateChecked(oSrc, args){
  if(document.all[“”].checked == false){
   alert(“Has to be checked.”);
   args.IsValid = false;
  <form id=”Form1″ method=”post” runat=”server”>
   <asp:CustomValidator ClientValidationFunction=”ValidateChecked” Runat=”server” ID=”val” />
   <asp:CheckBox ID=”chk” Runat=”server” />
   <asp:Button ID=”btn” Runat=”server” Text=”Submit” />

And the code-behind:

private void val_ServerValidate(object source, ServerValidateEventArgs args)


    args.IsValid = true;

    if(chk.Checked == false)

        args.IsValid = false;