Advertisement

09.05.2008 at 08:22AM PDT, ID: 23706565 | Points: 500
[x]
Attachment Details

Javascipt validation + radio buttons

Asked by pgooseman in JavaScript, Scripting Languages

I have a form with a few radio button selections.

Here is what I wouuld like to do.

Questions 1 & 2 are required.  However, if in question #1 Q1= A or Q1 =B then another radio selection poplulates and dislpays the three options for Q1a.  An option must be selected from Q1a if A or B was selected in Q1.  Of course if C is selected in Q1 then the options for Q1a are not displayed or requried.

<FORM>
<input type='radio' name='Q1' value='A' ID='Q1' >
<input type='radio' name='Q1' value='B' ID='Q1' >
<input type='radio' name='Q1' value='C' ID='Q1' >

      <input type='radio' name='Q1a' value='A' ID='Q1' >
      <input type='radio' name='Q1a' value='B' ID='Q1' >
      <input type='radio' name='Q1a' value='C' ID='Q1' >

<input type='radio' name='Q2' value='A' ID='Q2' >
<input type='radio' name='Q2' value='B' ID='Q2' >
<input type='radio' name='Q2' value='C' ID='Q2' >
</FORM>

Here is my script that does partially work however I need to include some type of IF statement validating the Q1a radio buttons if Q1 = A or B.


function CheckResponse()

{       
var radio_choice1 = false;
 
for (counter = 0; counter < Form1.Q1.length; counter++)
{
 
if (Form1.Q1[counter].checked)
radio_choice1 = true;
}

if (!radio_choice1)
{
 
alert("Please answer question 1.")
document.Form1.Q1.focus();
return (false);
}

 

var radio_choice1a = false;
 
for (counter = 0; counter < Form1.Q1a.length; counter++)
{
 
if (Form1.Q1a[counter].checked)
radio_choice1a = true;
}

if (!radio_choice1a)
{
 
alert("Please answer question 1a.")
document.Form1.Q1a.focus();
return (false);

}
   

var radio_choice2 = false;
 
for (counter = 0; counter < Form1.Q2.length; counter++)
{
if (Form1.Q2[counter].checked)
radio_choice2 = true;
}

if (!radio_choice2)
{
alert("Please answer question 2.")
document.Form1.Q2.focus();
return (false);
}

return (true);
} Start Free Trial
[+][-]09.05.2008 at 08:34AM PDT, ID: 22400100

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.05.2008 at 08:37AM PDT, ID: 22400143

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.05.2008 at 08:49AM PDT, ID: 22400286

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628