Hello im trying to get this checkbox code working, im using it in php in a while loop output so when the user gets there search output they can select an option by ticking a option but the user must at least select one option no more then 2 options cheers

<SCRIPT TYPE="text/javascript" LANGUAGE=JAVASCRIPT>

function checkCheckBoxes() {
	if (document.Dates.checkbox.checked == false &&	
            document.Dates.checkbox.checked == false &&	    
            document.Dates.checkbox.checked == false)
		{		
                  alert ('You didn\'t choose any of the checkboxes!');	
	          return false;		
                }	
     else		
                {		
                 return true;		
                }	
        }
//-->
</SCRIPT>

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

You have to find a way to evaluate each checkbox. I believe something like this would work.

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

hello it dnt work no message gets displayed if nothing is clicked. no error messages.

I would suggest using the Javascript try{ and catch() functions which will display what the error message is. I have included them in the code below:

<script type="text/javascript">
function checkCheckBoxes(){

try{
var checkSelected = false;

for (i = 0; i < document.Dates.checkbox.length; i++){

if (document.Dates.checkbox[i].checked){
checkSelected = true;
}

if (!checkSelected){
 alert('You didn\'t choose any of the checkboxes!');
 return false;
}
}
}catch(e){
alert("Error: " + e);
}
}
</script>

try the code and post back here what happens.

hello it jus says error:[object error] in a message box even if you click submit and no checkbox have been ticked if one has been ticked say message

<SCRIPT LANGUAGE="JavaScript">
<!--
	function validate(){

    var message="Please Enter";
	var noErrors =message
if(!document.form1.member.checked){
	message=message+"\n -->Agree to the terms";
	}
if (message == noErrors) {
return true;
}
else
{
alert(message);
return false;
}
}
-->
</SCRIPT> 

<html>
<body>
 <input name="member" type="checkbox" value="0" />
<body>
</html>

u can use for more than 1 also

for more than one

<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.