Hello ive got this code to check if a checkbox has been checked i want the user to at least check one checkbox, if they select none then an message should appear saying you havent selected any checkboxes. the user can only select up2 two checkboxes ive tried this code below and nothing is happening if any1 could help me plz

<script type="text/javascript">
function Check(){
var checkSelected = false;
for (i = 0; i < document.Dates.checkbox.length; i++)
{
if (document.Dates.checkbox.checked){
checkSelected = true;
}
if (!checkSelected)
{
alert('You didn't choose any of the checkboxes!');
return false;
}
}
}
</script>

<form name="Dates" action="send.php" method="post" onsubmit="return Check();">

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin

function validate() {
var theMessage = "Please complete the following: \n-----------------------------------\n";
var noErrors = theMessage
// Make sure at least 1 checkbox is checked
var multiCheckbox = false;
for (i = 0; i < document.form1.session.length; i++) {
if (document.form1.session[i].checked)
multiCheckbox = true; }
if (!multiCheckbox) {
theMessage = theMessage + "\n --> Choose which session(s)";
}

// If no errors, submit the form
if (theMessage == noErrors) {
return true;
} else {
// If errors were found, show alert message
alert(theMessage);
return false;
}
}
// End -->
</script> 
</head>
<body>

<form name="form1" action="http://www.google.com" onSubmit="return validate(this);">
<table width="300" border="0" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#cccccc">
<table width="100%" border="0" cellpadding="3" cellspacing="1">
<tr bgcolor="#FFFFFF">
<td>Which session(s) will you attend:<!-- field cannont be left blank --><br>
<input name="session" type="checkbox" value="morning">Morning<br>
<input name="session" type="checkbox" value="afternoon">Afternoon<br>
<input name="session" type="checkbox" value="evening">Evening</td>
</tr>
<tr bgcolor="#FFFFFF">
<td align="center"><input name="submit" type="submit" value="submit">
  <input name="reset" type="reset" value="reset">
</td>
</tr>
</table>
</td>
</tr>
</table>
</form> 
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.