Solved

javascript count checked checkboxes

Posted on 2008-10-20
5
946 Views
Last Modified: 2012-05-05
hi experts.
I have a form with two sets of checkboxes in the same form (unavoidable, client request).

I need to limit the number of boxes checked, in EACH set, to two.

The IDs of checkbox set #1 are: PR2a,PR2b,PR2c,PR2d,PR2e,PR2f,PR2g,PR2h,PR2i
The IDs of checkbox set #1 are: PR3a,PR3b,PR3c,PR3d,PR3e,PR3f,PR3g,PR3h,PR3i

I need my javascript to raise an alert
if the first set has more than two boxes checked
AND/OR
if the second set has more than two boxes checked

A separate function for each would be fine.

the ID of my form is "frm1", and my form calls the function(s) with the statement onclick="return checkit(this.form)"

Any help would be much appreciated. I am pretty good at VBScript, but a relative newbie at javascript.
0
Comment
Question by:tom_burkhardt
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:sh0e
ID: 22759653

function checkit(formid){
var id1 = "PR2a,PR2b,PR2c,PR2d,PR2e,PR2f,PR2g,PR2h,PR2i";
var id2 = "PR3a,PR3b,PR3c,PR3d,PR3e,PR3f,PR3g,PR3h,PR3i";
var count1=0, count2=0;
arr1 = id1.split(',');
arr2 = id2.split(',');
for(i=0;i<arr1.length;i++)
if(document.getElementById(arr1[i]).checked == 1)
count1++;
for(i=0;i<arr2.length;i++)
if(document.getElementById(arr2[i]).checked == 1)
count2++;
if(count1 > 2 || count2 > 2)
alert('more than 2 checked');
}

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22759709
try:
function checkit(f)
{
	var valid=true;
	var msg = "";
	var ids = [
			{"group":"PR2a,PR2b,PR2c,PR2d,PR2e,PR2f,PR2g,PR2h,PR2i","max":2,"checked":0, "label":"Group 1"}
			,{"group":"PR3a,PR3b,PR3c,PR3d,PR3e,PR3f,PR3g,PR3h,PR3i","max":2,"checked":0, "label":"Group 2"}
			]; 
	for( var i=0, limit=ids.length; i < limit ++i)
	{
		var e = ids.group.split(",");
		for( var j=0; j < e.length; ++j)
		{
			var t = document.getElementById( e[j] );
			if( t && t.checked )
			{
				++ids[i].checked;
			}
		}
		if( ids[i].checked > ids[i].max)
		{
			valid=false;
			msg += "You may choose at most " + ids[i].max+ " from " + ids[i].label +"\n";
		}
	}
	if( !valid )
	{
		alert(msg);
	}
return valid;
}

Open in new window

0
 

Author Comment

by:tom_burkhardt
ID: 22759753
pretty darn cool, thanks!!
0
 

Author Closing Comment

by:tom_burkhardt
ID: 31507889
You saved me literally hours of head-banging which is time I simply cannot afford right now. Thanks so much,
0
 
LVL 82

Expert Comment

by:hielo
ID: 22759831
you are welcome.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Refresh div via ajax without lost ccs <li> styles 8 32
HTML5 frame 5 25
Javascript_Set_dropdown_value 7 32
Grunt script for Build Process 1 25
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question