Click to See Complete Forum and Search --> : A group of checkbox inputs


Perfidus
01-17-2004, 07:54 AM
With this script I can force the user to select the checkbox before continuing, but now I have a menu with lots of checkboxes and I would like the user to select at least 2 of them; how can I check it?

<form name=formulario>
<INPUT type=checkbox name=casilla>
<INPUT type=button value="Salir"
onclick="Goingout()">
</form>

<script>
function Goingout(){
if (document.formulario.casilla.checked) window.close()
else alert("Select this option to continue")
}
</script>

Pittimann
01-17-2004, 08:28 AM
Hi!

One of many ways for that would be:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function Goingout(){
var count=0;
for (var i = 0; i <document.formulario.elements.length; i++){
if (document.formulario.elements[i].type=="checkbox"){
if (document.formulario.elements[i].checked==true){
count++;
}
}
}
if (count>=2) window.close()
else alert("Select at least two options to continue")
}
//-->
</script>
</head>
<body>
<form name=formulario>
<INPUT type=checkbox name=casilla1><br>
<INPUT type=checkbox name=casilla2><br>
<INPUT type=checkbox name=casilla3><br>
<INPUT type=button value="Salir" onclick="Goingout()">
</form>
</body>
</html>

Cheers - Pit