Validate group of checkboxes

G

Guest

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
 
S

sloan

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_CANNED_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_VALIDATION_FUNCTION_PREFIX +
checkBoxListID, strBuilder.ToString());
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top