473,387 Members | 1,464 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,387 software developers and data experts.

Validate group of checkboxes

Jon
Hello all,

I need to validate client-side a group of checkboxes to make sure at least 1
ic ticked. Does anyone have any examples please?

Thanks.

Jon
Sep 29 '06 #1
1 2121
If you're talking about a checkbox LIST, then try this:

private readonly string JAVA_SCRIPT_MULTIPLE_ERRORS_SEPERATOR_VAR_NAME =
"varMultipleErrorSeperator";
private readonly string JAVA_SCRIPT_INTERNAL_CANNED_PREFIX =
"INTERNALJAVASCRIPTCOMMAND_";
private readonly string JS_INDENT = " ";
private readonly string GENERIC_VALIDATION_FUNCTION_PREFIX = "validate_";
//the constuctor of my object takes the Page, and sets the ._currentPage
member variable.
That took a while to figure out, so maybe post a thank you on this one
/// <summary>
/// Validates that a checkboxlist has at least one item selected.
/// </summary>
/// <param name="cbl">The CheckBoxList.</param>
/// <param name="errorMsg">The error message.</param>
public void
ValidateDataAddCheckBoxListAtLeastOneItemSelected( System.Web.UI.WebControls.
CheckBoxList cbl, string errorMsg)
{
string checkBoxListID = cbl.ID;
_allItemToBeValidated.Add(JAVA_SCRIPT_INTERNAL_CAN NED_PREFIX +
checkBoxListID);
StringBuilder strBuilder = new StringBuilder();
strBuilder.Append("<script language='javascript'>" +
System.Environment.NewLine);
strBuilder.Append("<!-- " + System.Environment.NewLine);
strBuilder.Append("function " + GENERIC_VALIDATION_FUNCTION_PREFIX +
checkBoxListID + "(source) {" + System.Environment.NewLine);
strBuilder.Append("var objForm = document." + _parentForm.ID + ";" +
System.Environment.NewLine);
strBuilder.Append("var errorCount = 0; //true" +
System.Environment.NewLine);
strBuilder.Append("var objCheckBoxList = document.getElementById('" +
checkBoxListID + "');" + System.Environment.NewLine);
strBuilder.Append("if(objCheckBoxList == null) {" +
System.Environment.NewLine);
strBuilder.Append("alert ('There was an issue checking the *" +
checkBoxListID + "* check box list');" + System.Environment.NewLine);
strBuilder.Append("return errorCount;" + System.Environment.NewLine);
strBuilder.Append("}" + System.Environment.NewLine);
strBuilder.Append("var atLeastOneIndividualItemChecked = false;" +
System.Environment.NewLine);
strBuilder.Append("var i = 0;" + System.Environment.NewLine);
strBuilder.Append("var currentListItem =
document.getElementById(objCheckBoxList.id + '_' + i);" +
System.Environment.NewLine);
strBuilder.Append("while (currentListItem != null)" +
System.Environment.NewLine);
strBuilder.Append("{" + System.Environment.NewLine);
strBuilder.Append("//alert (currentListItem.name);" +
System.Environment.NewLine);
strBuilder.Append("//this loop uses the naming scheme of controlName_1,
controlName_2" + System.Environment.NewLine);
strBuilder.Append("//to check items inside the CheckBoxList" +
System.Environment.NewLine);
strBuilder.Append("//using the != null check should keep it from getting
locked up" + System.Environment.NewLine);
strBuilder.Append("if (currentListItem.checked == true) {" +
System.Environment.NewLine);
strBuilder.Append("atLeastOneIndividualItemChecked = true;" +
System.Environment.NewLine);
strBuilder.Append("}" + System.Environment.NewLine);
strBuilder.Append("//increment i AND get the next control" +
System.Environment.NewLine);
strBuilder.Append("i += 1 ;" + System.Environment.NewLine);
strBuilder.Append("currentListItem =
document.getElementById(objCheckBoxList.id + '_' + i);" +
System.Environment.NewLine);
strBuilder.Append(JS_INDENT + "}" + System.Environment.NewLine);
strBuilder.Append("if (atLeastOneIndividualItemChecked == false) { " +
System.Environment.NewLine);
strBuilder.Append(JS_INDENT + "source.errormessage += " +
JAVA_SCRIPT_MULTIPLE_ERRORS_SEPERATOR_VAR_NAME + " + '" +
CreateWebSafeJavaScriptMessage(this._currentPage , errorMsg) + "';" +
System.Environment.NewLine);
strBuilder.Append(JS_INDENT +
JAVA_SCRIPT_MULTIPLE_ERRORS_SEPERATOR_FUNCTION + "();" +
System.Environment.NewLine);
strBuilder.Append(JS_INDENT + "errorCount+=1;" +
System.Environment.NewLine);
strBuilder.Append("}" + System.Environment.NewLine);
strBuilder.Append("return errorCount;" + System.Environment.NewLine);
strBuilder.Append("}" + System.Environment.NewLine);
strBuilder.Append(System.Environment.NewLine + "// -->" +
System.Environment.NewLine);
strBuilder.Append("</script>");
if
((!(_currentPage.IsStartupScriptRegistered(GENERIC _VALIDATION_FUNCTION_PREFI
X + checkBoxListID))))
{
_currentPage.RegisterStartupScript(GENERIC_VALIDAT ION_FUNCTION_PREFIX +
checkBoxListID, strBuilder.ToString());
}
}
Sep 29 '06 #2

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

Similar topics

0
by: Kumar | last post by:
Hi , I am using the article code shown in below url: http://www.codeproject.com/aspnet/xmlxsltransformer.asp?df=100&forumid=118504&select=1170328 This article is very cool feature. I...
2
by: Jeff Robichaud | last post by:
Hi, This seems like a classic one (but I'm pretty new to Javascript)... I have a list of checkboxes for all the U.S. states and a checkbox "All" to check them all at once. The following...
2
by: Charles | last post by:
Ok, so I'm creating a form on the fly. I can create the option group, I can create the checkboxes, but I can't figure out how to bind them to the option group I'm creating them within. I was...
3
by: Egbert | last post by:
I use a System.Web.UI.WebControls.CheckBoxList object to fill a list. I need to label groups of checkboxes in the list by Category (Determined dynamically from the database) at runtime. Is it...
4
by: moondaddy | last post by:
There are different times when I will have a group of checkboxes and need to force only one to be checked at a time. I would also like to do this client side and not require a postback. These...
2
by: NishSF | last post by:
Would anyone have any suggestions/javascript code so that if one clicks the Radio Button "Yes" below he has the option of selecting any of the six CheckBox below. If the user clicks on Radio Button...
2
by: birwin | last post by:
I found a Javascript snippet that very effectively checks all boxes on a page, even on a page on which the input tags are in tables and on which I use a lot of other javascript. Unfortunately it is...
1
by: mbarnhizer | last post by:
Hello All, Trying to figure out how to validate a series of questions on an online test. I am thinking that VB or Javascript is the best route, but your input may make a difference. The site i...
6
by: dream2rule | last post by:
Hello All, I am trying to validate multiple checkboxes whose values are stored in an array using php. I have been trying from a really long time but nothing's working out. Can anyone help? ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.