472,145 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

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 12998
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Michael Goettsche | last post: by
reply views Thread by zoran | last post: by
2 posts views Thread by Fabrice | last post: by
3 posts views Thread by Hukkky | last post: by
reply views Thread by Saiars | last post: by

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.