By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,041 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.