Solved

javascript count checked checkboxes

Posted on 2008-10-20
5
942 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now