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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple radio-buttons

Status
Not open for further replies.

Quasibobo

Programmer
Oct 11, 2001
168
NL
Hi

I need to validate a page with a lot of radiobuttons (dynamicly created bij PHP):
Code:
<form name="svl">
<input type="radio" name="question_1" value="3">
<input type="radio" name="question_1" value="2">
<input type="radio" name="question_1" value="1">
<input type="radio" name="question_2" value="3">
<input type="radio" name="question_2" value="2">
<input type="radio" name="question_2" value="1">
<input type="radio" name="question_3" value="3">
<input type="radio" name="question_3" value="2">
<input type="radio" name="question_3" value="1">
...
<input type="button" value="Submit" onClick="return Validate(); document.svl.submit();">
</form>


and so on... exactly 10 radiogroups per page. To validate if one of the radiobuttons in each group is selected I wrote this:

Code:
function Validate()
{
for (i=1;i<11;i++)
 {
  for (j=0;j<3;j++)
   {
   if(document.svl.question_'+i'[j].checked == true)
    {
    return (true);
    }
   else
    {
    alert ('Please make a choice in every group');
    return (false);
    }
   }
 }
}

But this isn't working... I've looked around on the internet but I can't get a solution to my problem.

Anyone?

T.I.A.

Don't eat yellow snow!
 
You have to write a loop for each individual group. Try something like this:
Code:
function Validate() {
   for (i = 1; i < 11; i++) {
      radioGroup = document.forms["svl"].elements["question_" + i];
      var radioChecked = false;
      for (j = 0; j < radioGroup.length; j++) {
         radioChecked = (radioGroup[j].checked) ? true : radioChecked;
      }
      if (!radioChecked) {
         alert("You must select something from radio group " + i);
         return false;
      }
   }
   return true;
}

-kaht

banghead.gif
 
Ok... thanks... exactly what I ment to do.

Don't eat yellow snow!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top