i have some checkbox:
i.e:

<input type="checkbox" name="a" value="a" onclick="check(this)" />a
<input type="checkbox" name="b" value="b" onclick="check(this)" />b
<input type="checkbox" name="c" value="c" onclick="check(this)" />c
<input type="checkbox" name="d" value="a" onclick="check(this)" />a

if user select >= 2choices, the other checkbox become disable and < 2choices become enable.. so user max choose 2 choices.. if user not choose alert will show...

anybody can give a sample code like this.. i've try in googling but i can't find like this problem..

thanks,

Ardy Satria - forzadraco

Recommended Answers

All 4 Replies

Hi forzadraco!

I have the following code for you.Please see if this code serve your purpose. If you are satisfied with the code , please mark it Solved.

<SCRIPT language=JavaScript type=text/javascript>
function check(){
	var i;
	var j=0;
	var chk;
	for(i=1;i<=4;i++){
		chk=document.getElementById("a" + i).checked;
		if(chk!=false){
			j=j+1;
			if(j>2){
				document.getElementById("a" + i).checked=false;
				alert("You cannot choose more than two options");
			}
		}
	}
}
</SCRIPT>
<FORM>
	<INPUT id="a1" name="a1" type=checkbox onClick="check()">A<BR>
	<INPUT id="a2" name="a2" type=checkbox onClick="check()">B<BR>
	<INPUT id="a3" name="a3" type=checkbox onClick="check()">C<BR>
	<INPUT id="a4" name="a4" type=checkbox onClick="check()">D
</FORM>

First you add the JavaScript code and then add the HTML form code.

With Regards

Rupam Datta

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.