Limit CheckBox Items Checked

[Total: 0    Average: 0/5]

This limits checkbox selections to 2.

Body Code:

This script only allows 2 items to be selected. it does not matter which 2.
<script>
var MAX_ALLOWED=2;
var clickedData=new Array(false,false,false,false);
function itemsClicked(){var i=0;for(var j=0;j<clickedData.length;j++)i+=clickedData[j]?1:0;return(i);}
function itemClicked(_v)
{
  var ALLOW_THIS=true;
  var x=itemsClicked();
  if (x>=MAX_ALLOWED && !clickedData[_v])
  {
    ALLOW_THIS=false;
  }
  else
  {
    clickedData[_v]=clickedData[_v]?false:true;
	eval("document.exf1.r"+_v+".clicked=false;");
  }
  document.showit.s1.value=x; /* comment out */
  return (ALLOW_THIS); /* kill the event handler */
}


</script>

<form name=exf1>
<INPUT name=r1 type=checkbox value=Charity_profile onclick="return itemClicked(1)">Item 1<BR>
<INPUT name=r2 type=checkbox value=lost_found      onclick="return itemClicked(2)">Item 2<BR>
<INPUT name=r3 type=checkbox value=fun_section     onclick="return itemClicked(3)">Item 3<BR>
<INPUT name=r4 type=checkbox value=contacts        onclick="return itemClicked(4)">Item 4<BR>
</form>

<!-- the following form is just to show off how many is selected -->
<form name=showit>
<input type=text name=s1 value=0>
</form>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *