I modified the code so that it could verify any number of checkboxes:
HTML Code:
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Make sure at least one check-box is selected.</title>
</head>
<script language="JavaScript" type="text/javascript"><!--
function AtLeastXChecked ( nMinChecked, sListOfCheckIDs ) {
if ( ! document.getElementById ) return true;
sListOfCheckIDs = sListOfCheckIDs.split ( ',' );
var nNumChecked = 0;
for ( var i = 0 ; i < sListOfCheckIDs.length ; i ++ ) {
if ( document.getElementById ( sListOfCheckIDs [ i ] ).checked ) {
nNumChecked ++;
}
}
if ( nNumChecked < nMinChecked ) alert ( 'Dude! You like, *totally* need to have at least ' + nMinChecked + ' of those check-boxes selected!' );
return nNumChecked >= nMinChecked;
}
// --></script>
<body>
<form method="post" onsubmit="return AtLeastXChecked ( 2, 'chk1,chk2,chk3,chk4,chk5' );">
<ul style="list-style: none; margin: 0; padding: 0 0 15px 0;">
<li><input id="chk1" type="checkbox" name="chk" value="1" /> <label for="chk1">Option #1</label></li>
<li><input id="chk2" type="checkbox" name="chk" value="2" /> <label for="chk2">Option #2</label></li>
<li><input id="chk3" type="checkbox" name="chk" value="3" /> <label for="chk3">Option #3</label></li>
<li><input id="chk4" type="checkbox" name="chk" value="4" /> <label for="chk4">Option #4</label></li>
<li><input id="chk5" type="checkbox" name="chk" value="5" /> <label for="chk5">Option #5</label></li>
</ul>
<input type="submit" />
</form>
</body>
</html>
Hope this helps!
Bookmarks