Check the below sample code:
Code:
<SCRIPT LANGUAGE="javascript">
var lastElement = 0;
retArr = new Array();
function selectOnlyTwo(buttonGroup) {
if(buttonGroup.checked){ //alert('CHKD'+retArr);
retArr[retArr.length] = buttonGroup.id;
if(retArr.length>=3){
var na = document.getElementById(retArr[0]);
retArr.splice(0,1);
na.checked=false;
//return false;
}
}else{ //alert('ELSE'+retArr);
for(i=0;i<retArr.length;i++){
if(retArr[i]==buttonGroup.id){
retArr.splice(i,1);
}
}
}
} //return retArr;
//http://www.htmlgoodies.com/tutorials/forms/article.php/3479181/Checkboxes-Only-Two.htm?comment=38873-0
</script>
<FORM NAME="joe">
<b>Pick Only Two Please!</b>
<INPUT TYPE="checkbox" NAME="d0" id="d0"onclick="selectOnlyTwo(this)" > Hampster
<INPUT TYPE="checkbox" NAME="d1" id="d1" onclick="selectOnlyTwo(this)"> Dog
<INPUT TYPE="checkbox" NAME="d2" id="d2" onclick="selectOnlyTwo(this)"> Cat
<INPUT TYPE="checkbox" NAME="d3" id="d3" onclick="selectOnlyTwo(this)"> Pig
<INPUT TYPE="checkbox" NAME="d4" id="d4" onclick="selectOnlyTwo(this)"> Ferret
<INPUT TYPE="checkbox" NAME="d5" id="d5"onclick="selectOnlyTwo(this)"> Hampster
<INPUT TYPE="checkbox" NAME="d6" id="d6"onclick="selectOnlyTwo(this)"> Hampster
<INPUT TYPE="checkbox" NAME="d7" id="d7"onclick="selectOnlyTwo(this)"> Hampster
<INPUT TYPE="checkbox" NAME="d8" id="d8"onclick="selectOnlyTwo(this)"> Hampster
</FORM>
Thanks,
Bookmarks