...

View Full Version : Form Validating A Radio Button



tomyknoker
12-10-2007, 08:03 PM
Hi all... I have a group 2 Radio buttons, if the user selects the 2nd Radio button then they need to enter some data into a text field... The following only works for either Radio buttons... How can I specify it to be only the 2nd Radio button?


if(!document.form.radio1.checked && document.form.q3.value=="")
{
theMessage = theMessage + "\n --> You selected no we need a reason)";
}

Philip M
12-10-2007, 08:20 PM
Hi all... I have a group 2 Radio buttons, if the user selects the 2nd Radio button then they need to enter some data into a text field... The following only works for either Radio buttons... How can I specify it to be only the 2nd Radio button?


if(!document.form.radio1.checked && document.form.q3.value=="")
{
theMessage = theMessage + "\n --> You selected no we need a reason)";
}

if((!document.form.radio1.checked) && document.form.q3.value=="")

tomyknoker
12-10-2007, 08:37 PM
Hi there... But the thing is with this I have 2 Radio buttons in this group 'radio1' If the first one is selected, then the user doesn't need to fill out the textfield q3... The above code makes them fill it out wether they select the Yes OR No radio in that group...

arnyinc
12-10-2007, 08:59 PM
Instead of checking whether it is "checked", check the value of the radio button that is checked.

tomyknoker
12-11-2007, 12:05 AM
Ahhhh good idea, I did that but no luck... Even if I just have the radio validation part there it ignores it:


if((document.form.radio1.value=="No") && document.form.q3.value=="") {

And these are my radio buttons:


<input type="radio" name="radio1" id="yes" value="Yes">
</label>
Yes&nbsp;&nbsp;
<label>
<input type="radio" name="radio1" id="yes2" value="No">
</label>
No

Philip M
12-11-2007, 07:48 AM
if((!document.form.radio1[0].checked) && document.form.q3.value=="")

Javascript addresses each radio button using the array name and index number (starting at 0).

You did not post all you code before.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum