473,388 Members | 1,322 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Validating a checkbox

I would like to validate that a Checkbox is checked on the client-side.

I have tried the "requiredfieldvalidator" and the
"regularexpressionvalidator", but haven't been able to get either of them to
work the Checkbox.

Any suggestions?

Thanks,
Scott
Nov 18 '05 #1
2 3726
Scott,
If you have tried the RequiredFieldValidator, you would have seen:
Control 'chk' referenced by the ControlToValidate property of
'RequiredFieldValidator1' cannot be validated.

Indeed, the built-in validator controls cannot validate the checkbox. You
will have to use the custom validator to do this or write your own client
side validation.

A quick search of Google for "asp.net validate checkbox", you would find
that one of the results is:
http://msdn.microsoft.com/library/de...ercontrols.asp,
and this explains how validations work.

Here's a page that validates a checkbox:

<%@ 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>

Best regards,
Jeffrey Palermo

"Scott Natwick" <no**********@yahoo.com> wrote in message
news:i4********************@comcast.com...
I would like to validate that a Checkbox is checked on the client-side.

I have tried the "requiredfieldvalidator" and the
"regularexpressionvalidator", but haven't been able to get either of them to work the Checkbox.

Any suggestions?

Thanks,
Scott

Nov 18 '05 #2
Jeffrey,

Thank you for your suggestion. I used your example and it works!

Regards,
Scott

"Jeffrey Palermo [MCP]" <http://dotnetjunkies.com/weblog/jpalermo> wrote in
message news:uM**************@TK2MSFTNGP11.phx.gbl...
Scott,
If you have tried the RequiredFieldValidator, you would have seen:
Control 'chk' referenced by the ControlToValidate property of
'RequiredFieldValidator1' cannot be validated.

Indeed, the built-in validator controls cannot validate the checkbox. You
will have to use the custom validator to do this or write your own client
side validation.

A quick search of Google for "asp.net validate checkbox", you would find
that one of the results is:
http://msdn.microsoft.com/library/de...ercontrols.asp,
and this explains how validations work.

Here's a page that validates a checkbox:

<%@ 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>

Best regards,
Jeffrey Palermo

"Scott Natwick" <no**********@yahoo.com> wrote in message
news:i4********************@comcast.com...
I would like to validate that a Checkbox is checked on the client-side.

I have tried the "requiredfieldvalidator" and the
"regularexpressionvalidator", but haven't been able to get either of them

to
work the Checkbox.

Any suggestions?

Thanks,
Scott


Nov 18 '05 #3

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

Similar topics

3
by: Mark | last post by:
Hi, Im trying to validate a form, all the validating works apart from one field. This particular field must consist of the first 2 characters as letters, & the following 5 as numbers. And if it...
1
by: Miguel Orrego | last post by:
Hi, I have a form that is created dynamically from a recordset, this creates a line for each record together with 2 checkboxes, Yes & No so the source output would be something like this: ...
5
by: Stephen | last post by:
Could someone please help me with some validation. I have to write code which checks to see whether a dropdown list is populated with a value or a checkbox is checked. I want the code to run on the...
3
by: Lars Netzel | last post by:
Hi How do I validate a CheckBox with a formvalidator from ASP.NET ? I can't find anyway to do it and I need the look of this validation to look like the rest of the textbox validators on the...
4
by: Mike | last post by:
I'm having a problem which I thought would be a very simple thing but is turning out to be a difficult challenge. I have a web form with several input fields and I have a single checkbox, not a...
5
by: Bob | last post by:
In a datagridview (vs2005, VB.net) I have two columns that are checkboxes. I need to check that only one of the two can be checked. Its not permissible to have the two selected to true, but they...
7
by: mc | last post by:
I've not been able to get a Check box to client side validated on postback. with my changes I can get it to work on the serverside ok but not on the client. I have created a new control as...
0
by: Ned Balzer | last post by:
Hi, Can anyone point me in the direction of a solution for validating multiple checkboxes in an asp.net 2.0 page? 1) This is not a checkboxlist, it is a number of separate checkboxes 2) I do...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.