<script type="text/javascript"> function chkcontrol(j) { var sum=0; for(var i=0; i < document.form1.ckb.length; i++){ if(document.form1.ckb[i].checked){ sum = sum + parseInt(document.form1.ckb[i].value); } document.getElementById("msg").innerHTML="Sum :"+ sum; if(sum >10){ sum = sum - parseInt(document.form1.ckb[j].value); document.form1.ckb[j].checked = false ; alert("Sum of the selection can't be more than 10") //return false; } document.getElementById("msg").innerHTML="Sum :"+ sum; } } </script>
<form name=form1 method=post action=check.php> <table class='table table-striped' > <tr ><th >Select</th></tr> <tr ><td ><input type=checkbox name=ckb value=1 onclick='chkcontrol(0)';> Value = 1 </td></tr> <tr><td ><input type=checkbox name=ckb value=2 onclick='chkcontrol(1)';> Value = 2 </td></tr> <tr ><td ><input type=checkbox name=ckb value=3 onclick='chkcontrol(2)';> Value = 3 </td></tr> <tr ><td ><input type=checkbox name=ckb value=4 onclick='chkcontrol(3)';> Value = 4 </td></tr> <tr ><td ><input type=checkbox name=ckb value=5 onclick='chkcontrol(4)';> Value = 5 </td></tr> <tr ><td ><input type=checkbox name=ckb value=6 onclick='chkcontrol(5)';> Value = 6 </td></tr> <tr ><td ><input type=checkbox name=ckb value=7 onclick='chkcontrol(6)';> Value = 7 </td></tr> <tr ><td ><input type=checkbox name=ckb value=8 onclick='chkcontrol(7)';> Value = 8 </td> </tr> </table></form> Sum of the values can't exceed 10. <br><br> <div id=msg></div>
Post your comments , suggestion , error , requirements etc here