954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

CHECKBOX VAILDATION CODE not working

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>
aran87
Light Poster
42 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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>
buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
 

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

aran87
Light Poster
42 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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.

phper
Posting Whiz in Training
213 posts since Nov 2006
Reputation Points: 22
Solved Threads: 19
 

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

aran87
Light Poster
42 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 
<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

queenc
Junior Poster
145 posts since Mar 2008
Reputation Points: 9
Solved Threads: 4
 

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 -->
<input name="session" type="checkbox" value="morning">Morning
<input name="session" type="checkbox" value="afternoon">Afternoon
<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>
queenc
Junior Poster
145 posts since Mar 2008
Reputation Points: 9
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You