Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

formchecking with check boxes 1

Status
Not open for further replies.

meeble

Programmer
Sep 24, 2002
137
GB
I have 10 check boxes on my form. I want the user to have to tick at LEAST one of them.

How do I do this please?
 
Give the checkboxes the same id. Then you can:

Code:
function checkCheckboxes()
{
 var cbs = document.getElementById("idOfMyCheckboxes");
 var noneChecked = true;
 for(var i=0; i<cbs.length; i++)
 {
  if(cbs[i].checked)
  {
   noneChecked = false;
   break;
  }//end if
 }//end for

 if(noneChecked)
 {
  alert('You must check at least one checkbox!');
  return false;
 }//end if
 else
  return true;
}//end checkCheckboxes()

Something like that anyway.

'hope that helps.

--Dave
 
Hello,

I have done this and put onsubmit="return(checkCheckboxes())" in the form tag but even if I check a box it still says I must check at least one box.

What is wrong?

Thanks

James
 
Since you said you have 10 checkboxes on the page and you wanna check all of them, try this out:
Code:
<script language=JavaScript>

function checkFunction() {
   var checks = document.getElementsByTagName("input");
   for (i = 0; i < checks.length; i++) {
      if (checks[i].type == "checkbox") {
         if (checks[i].checked) {
            return (true);
         }
      }
   }
   return (false);
}

</script>
<body>
<form name=blahForm onsubmit='alert ((checkFunction()) ? "passed" : "failed")'>
<input type=checkbox>Apples<br>
<input type=checkbox>Oranges<br>
<input type=checkbox>Bananas<br>
<input type=checkbox>Pears<br>
<input type=checkbox>Cherries<br>
<input type=checkbox>Grapes<br>
<input type=submit value='Submit'>
</form>
</body>
Take note that you'll want to replace my onsubmit function with this instead:
onsubmit='return checkFunction()'
I left the alert in so that you could see it worked


-kaht

banghead.gif
 
This works on it's own but I need to do several functions in the onSubmit:

I have:

onsubmit="return (checkban() && checkSubmit() && checkFunction());"

Is this syntax wrong? It does the first two functions but ignores the last one.

Thanks

James
 
Are you sure it ignores the last one? Try putting the functions in one at a time and seperately make sure they are all validating correctly. Instead of putting onsubmit='return checkFunction()' try instead onsubmit='alert (checkFunction())'. Do that with each function one at a time so that you can see what boolean value is getting passed back. Also, make sure that checkban or checksubmit do not change the state of any of the elements on the page. If checkban for some reason unchecked the checkboxes, checkFunction will return false. If it still doesn't work post a 'view source' from your page so that I can try it in my own browser.

-kaht

banghead.gif
 
Funny enough it works when I change the functions round
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top