Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Here is Code (C#) for CheckBox Validation on Server and Client

112 views
Skip to first unread message

Earl Teigrob

unread,
Feb 6, 2004, 5:14:34 PM2/6/04
to
I wanted my "Terms and Conditions" Checkbox control to participate in my
ASP.NET validation just like all the the other controls on the page. After
some time of searching the web for an example of how to do this, I created
the script to do it and thought I would share it. Its a littel messy but
does the job. If anyone has a better solution, please let me know.

//Client Site Event Handler to put in Page_Load event
Page.RegisterClientScriptBlock("ValidateCheckBox1","<SCRIPT
LANGUAGE=\"JavaScript\">function validateCheckBox1(oSrc, args){args.IsValid
= document.all[\""+ CheckBox1.ID +"\"].checked;}</SCRIPT>");


//Server Site Event Handler
private void CustomValidator1_ServerValidate(object source,
System.Web.UI.WebControls.ServerValidateEventArgs args)
{
if (CheckBox1.Checked)
args.IsValid=true;
else
args.IsValid=false;
}

//Page Controls
<td>
<p>
<asp:CheckBox id="CheckBox1" runat="server"
Checked="False"></asp:CheckBox><FONT color="#ff3333">*</FONT>Agree
to Terms
<asp:CustomValidator id="CustomValidator1"
ClientValidationFunction="validateCheckBox1" runat="server"
ErrorMessage="[Agree to Terms] must be
checked">[Required]</asp:CustomValidator>
</p>
</td>


Peter Blum

unread,
Feb 7, 2004, 3:45:22 PM2/7/04
to
You've done the right thing for the server side. You don't have support on
the client-side. Its not hard to write the client-side for this. Here's some
simple logic to do it:

document.all['CheckBox1'].checked <- returns true or false

You should follow the .net docs on CustomValidators to learn how to enclose
that code within a client-side function.

FYI: I have built a replacement to Microsoft's validators that overcomes its
numerous limitations. "Professional Validation And More"
(http://www.peterblum.com/vam/home.aspx) includes a CheckedStateValidator
amongst the 22 validators it has. All of them have client-side support and
work on more browsers than Microsoft's (MS is only IE; mine works on IE,
Netscape/Mozilla, Opera 7 and Safari).

--- Peter Blum
www.PeterBlum.com
Email: PLB...@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Earl Teigrob" <earl...@hotmail.com> wrote in message
news:eYEhE7P7...@TK2MSFTNGP12.phx.gbl...

Earl Teigrob

unread,
Feb 9, 2004, 12:21:49 AM2/9/04
to
Peter,

Thanks for your feedback. Now am I missing something? I do have the follow
script to do the Client side validation that includes the
document.all['CheckBox1'].checked function you mention...

> Page.RegisterClientScriptBlock("ValidateCheckBox1","<SCRIPT
> LANGUAGE=\"JavaScript\">function validateCheckBox1(oSrc,
args){args.IsValid
> = document.all[\""+ CheckBox1.ID +"\"].checked;}</SCRIPT>");

I just wrote it out dynamically so I could set the CheckBox1 Id property.

I will certainly check out your validators you have written...they are a
pain to write from scratch every time...

Earl


"Peter Blum" <PLB...@Blum.info> wrote in message
news:Ody%23Rtb7D...@TK2MSFTNGP10.phx.gbl...

Peter Blum

unread,
Feb 9, 2004, 11:37:07 PM2/9/04
to
One change I'd make to your code. Always provide the ClientID of the control
into the javascript, not the ID property.

--- Peter Blum
www.PeterBlum.com
Email: PLB...@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Earl Teigrob" <earl...@hotmail.com> wrote in message

news:%23lJ7Ozs...@TK2MSFTNGP11.phx.gbl...

0 new messages