473,466 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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>
Nov 18 '05 #1
3 13115
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Earl Teigrob" <ea******@hotmail.com> wrote in message
news:eY**************@TK2MSFTNGP12.phx.gbl...
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>

Nov 18 '05 #2
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" <PL****@Blum.info> wrote in message
news:Od****************@TK2MSFTNGP10.phx.gbl... 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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Earl Teigrob" <ea******@hotmail.com> wrote in message
news:eY**************@TK2MSFTNGP12.phx.gbl...
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>


Nov 18 '05 #3
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Earl Teigrob" <ea******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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" <PL****@Blum.info> wrote in message
news:Od****************@TK2MSFTNGP10.phx.gbl...
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Earl Teigrob" <ea******@hotmail.com> wrote in message
news:eY**************@TK2MSFTNGP12.phx.gbl...
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>



Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael Goettsche | last post by:
Hi there, I'm trying to write a simple server/client example. The client should be able to send text to the server and the server should distribute the text to all connected clients. However, it...
0
by: zoran | last post by:
Hi, I have a simple TCP server client in .net and for some reason the Accept() function in the Server part never gets called and never returns a new socket. This thing has been driving me nuts for...
2
by: Fabrice | last post by:
Hello, For security reasons, i'd like to permit validation on client and server side. But if JavaScript is desactivate on the client, the validation have to take place on server side for catch...
6
by: MaxPersson | last post by:
Hi guys! My first post here is ofcourse a little problem of mine! =) ive been looking pretty mutch everywhere, asking my proffesor and such, and we cant figure this out at all. I even analyzed the...
5
by: divingIn | last post by:
Hi, I have a button control that has both server/client side events. There is a validation i'm doing in the client side "onclick" event that opens up an alertbox. Is there a way i can suppress the...
0
by: 85ssp | last post by:
I am creating a small server client program that is meant for up to 70 connections from 70 different computers on a network. Everything in the program functions correctly except when testing...
3
by: Hukkky | last post by:
I'm testing simple server/client codes on linux. just server can wait for client's connect sign and accept, and client can't connect to server, this is all. There's no problems just for this...
3
by: desire83 | last post by:
hello, im new in c#.net... i have made a server application and a client application for the same machine...the server is suppos to send a string to client when the button on the server application...
2
by: fredszky | last post by:
Hello I am very new to perl, however i managed to make this server/client work with udp, now i would like to do the same thing but with TCP/IP, what must i do? Server: #!perl -w # Server...
3
by: supriya prasad | last post by:
I am trying to set up a server client network .Server will be able to stream live audio captured from a radio receiver connected to its line in port to multiple clients over the network. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.