Actually I like to use AJAX for Checkbox validation, if you add the checkboxes inside an UpdatePanel and then on the server side you can check if they are check or not and update a red label by the checkbox. To the user, looks like works the same way the validation,
for you, is just a little bit more magic to write.
You're welcome, DannyDep. Let me also point you (and others) to an accompanying article, which dissects and discusses the custom, compiled server control (skmValidators):
function CheckBoxesValidations() {
if (document.form1.CHKBOX1.checked == false &&
document.form1.CHKBOX2.checked == false &&
document.form1.CHKBOX3.checked == false)
{
alert (' you have not choose any of the checkboxes!');
return false;
}
else
{
return true;
}
}
Here's a solution that I used in a recent website:
Since the asp:CustomValidator
does not accept an asp:CheckBox
as the control to validate, the first step is to set it to any other control in the page (e.g. a textbox).
The second step is to write a bit of JavaScript (JQuery) to return the non-checked state of the checkbox to validate. Below, I am showing a JavaScript variable being set, that is for use by the JavaScript method.
<
script
type="text/javascript">
var
controlID1 = '<%= CheckBoxAgree.ClientID %>';
</
script>
Here's the JavaScript function you'll need to write:
<asp:CheckBox
ID="CheckBoxAgree"
runat="server"
Text="I confirm that the above information is accurate to the best of my knowledge and agree to the terms and
conditions" />
<asp:CustomValidator
ID="CustomValidatorAgree"
runat="server"
ClientValidationFunction='IsNotChecked1'
onservervalidate="CustomValidatorAgree_ServerValidate"
ValidationGroup="group1"
ControlToValidate="TextBoxTelephone"
ErrorMessage="'I confirm' must be checked"
ValidateEmptyText="True"
></asp:CustomValidator>
Note that above we are setting the control to validate to any other control in the page.
Since you must also validate on the server side (best practice), here's the C# code:
DannyDep
Member
749 Points
316 Posts
Checkbox Validation - Best Solution
Dec 13, 2006 07:07 PM|LINK
I've spent a couple of hours searching for a solution to validating a checkbox, since the RequiredFieldValidator does not work with Checkboxes.
Here is a great solution.
Look for "skmValidators - custom validation controls for CheckBoxes and CheckBoxLists for ASP.NET 2.0."
http://scottonwriting.net/sowBlog/CodeProjects.htm
Thanks Scott.
albertpascua...
All-Star
17520 Points
3475 Posts
MVP
Re: Checkbox Validation - Best Solution
Dec 16, 2006 03:52 PM|LINK
Al
My Blog
moredotnet
Contributor
4685 Points
887 Posts
Re: Checkbox Validation - Best Solution
Dec 18, 2006 06:01 AM|LINK
Hi
If your just validating that checkbox is checked use this javascript snippet
document.getElementById("cb").checked == true
If you want to check & uncheck all checkboxes at one go...use this
function check(field)
{
if (checkflag == "false")
{
for (i = 0; i < field.length; i++)
{
if(field[i].disabled == false)
field[i].checked = true;
}
checkflag = "true";
return "Uncheck All";
}
else
{
for (i = 0; i < field.length; i++)
{
if(field[i].disabled == false)
field[i].checked = false;
}
checkflag = "false";
return "Check All";
}
}
Cheers
moredotnet
http://moredotnet.googlepages.com
BOOK: .NET INTERVIEW CRACKERJACK
WEBSITE: ASP.NET, C#, AJAX, SQL, Design Patterns
Scott Mitche...
Contributor
4114 Points
712 Posts
ASPInsiders
MVP
Re: Checkbox Validation - Best Solution
Dec 20, 2006 03:09 PM|LINK
You're welcome, DannyDep. Let me also point you (and others) to an accompanying article, which dissects and discusses the custom, compiled server control (skmValidators):
checkbox
-- Scott Mitchell
-- mitchell@4guysfromrolla.com
-- http://scottonwriting.net/sowblog/
-- http://www.4GuysFromRolla.com/ScottMitchell.shtml
sunilyadav16...
Participant
1955 Points
370 Posts
Re: Checkbox Validation - Best Solution
Mar 12, 2009 07:24 AM|LINK
Hi,
Here is the code using javascript....
<html>
<head>
<title> Checkbox validations</title>
<SCRIPT TYPE="text/javascript">
function CheckBoxesValidations() {
if (document.form1.CHKBOX1.checked == false &&
document.form1.CHKBOX2.checked == false &&
document.form1.CHKBOX3.checked == false)
{
alert (' you have not choose any of the checkboxes!');
return false;
}
else
{
return true;
}
}
</SCRIPT>
</head>
<body>
<form onsubmit="return CheckBoxesValidations() ;" name="form1" action="">
<input type="checkbox" name="CHKBOX1" value="1">1</p>
<input type="checkbox" name="CHKBOX2" value="2">2</p>
<input type="checkbox" name="CHKBOX3" value="3">3</p>
<input type="submit" value="Submit!" />
</form>
</body>
</html>
If you like my post mark it as ANSWER.
Regards,
sunil
Follow me here
ajith P T
Member
131 Points
76 Posts
Re: Checkbox Validation - Best Solution
May 05, 2009 07:12 AM|LINK
You can use Customevalidator for validating the checkbox
Manager Technology
Sofker Solutions Pvt. Ltd.
Cochin
kerala, India
nalinbc
Member
6 Points
4 Posts
Re: Checkbox Validation - Best Solution
May 05, 2009 11:28 AM|LINK
Here's a solution that I used in a recent website:
Since the asp:CustomValidator does not accept an asp:CheckBox as the control to validate, the first step is to set it to any other control in the page (e.g. a textbox).
The second step is to write a bit of JavaScript (JQuery) to return the non-checked state of the checkbox to validate. Below, I am showing a JavaScript variable being set, that is for use by the JavaScript method.
<
script type="text/javascript">var
controlID1 = '<%= CheckBoxAgree.ClientID %>';</
script>Here's the JavaScript function you'll need to write:
function
IsNotChecked1(obj, args) { var checkbox = $("#" + controlID1); args.IsValid = checkbox.attr('checked');}
Here's the code to include in the aspx page:
<asp:CheckBox ID="CheckBoxAgree" runat="server" Text="I confirm that the above information is accurate to the best of my knowledge and agree to the terms and conditions" />
<asp:CustomValidator ID="CustomValidatorAgree" runat="server" ClientValidationFunction='IsNotChecked1' onservervalidate="CustomValidatorAgree_ServerValidate" ValidationGroup="group1" ControlToValidate="TextBoxTelephone" ErrorMessage="'I confirm' must be checked" ValidateEmptyText="True" ></asp:CustomValidator>Note that above we are setting the control to validate to any other control in the page.
Since you must also validate on the server side (best practice), here's the C# code:
protected void CustomValidatorAgree_ServerValidate(object source, ServerValidateEventArgs args){
args.IsValid = (CheckBoxAgree.Checked);
}
Best regards,
- Nalin Jayasuriya
CustomValidator CheckBox Checked
Spider Maste...
Participant
1664 Points
483 Posts
Re: Checkbox Validation - Best Solution
May 29, 2009 03:55 AM|LINK
I have created my own Checkbox and checkboxlist validator if your interested.
It works just the same as all <asp:Validators and works with the validation summary also.
You just add the dll to your toolbox and drop it on the page, set the properties and go nuts [:D]
Trading Center is a Continuation of the Classifieds Starter Kit onCode Plex.
shahed.kazi
All-Star
17955 Points
3636 Posts
Re: Checkbox Validation - Best Solution
May 30, 2009 05:51 AM|LINK
You can use a compare validator for the checkbox checking the values of the checkbox.
.NET World |Captcha Control
sunrobin23
Member
4 Points
2 Posts
server control and custom control differences
Jun 11, 2009 01:13 PM|LINK
what is server control and custom control diff.?