Jeffrey Palermo (.com)

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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" >
<HTML>
 <HEAD>
  <script language="javascript">
 function ValidateChecked(oSrc, args){
  if(document.all["<%=chk.ClientID%>"].checked == false){
   alert("Has to be checked.");
   args.IsValid = false;
  }
 }
  </script>
 </HEAD>
 <body>
  <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" />
  </form>
 </body>
</HTML>

And the code-behind:

private void val_ServerValidate(object source, ServerValidateEventArgs args)

{

    args.IsValid = true;

    if(chk.Checked == false)

        args.IsValid = false;

}


Posted 11-11-2004 7:26 PM by Jeffrey Palermo

[Advertisement]

Comments

Zencat wrote re: CustomValidator - level 200
on 12-29-2004 12:51 AM
Many thanks for the tip - it made things real easy for me! :)
DB wrote re: CustomValidator - level 200
on 02-07-2005 4:24 AM
How Do I create a dynamic customvalidator for a dynamically created checkbox