Solved

javascript count checked checkboxes

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

757 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

18 Experts available now in Live!

Get 1:1 Help Now