Click to See Complete Forum and Search --> : validating a form radio button


SeanPaul72
08-10-2003, 04:55 PM
I have the following radio button in a form<tr><td valign=top><i>1</i></td><td valign=top><i>Are there any judgments against you?</i>
<br></td>
<td align=center valign=top><input type=radio name="L230" value="Y"></td>
<td align=center valign=top><input type=radio name="L230" value="N"></td>
</tr>

My on submit handler looks like this:<form action=caddress.php name=lion1003 method=post onSubmit="return DataValSubmit('declarations')">

I have tried to vlidate the button searching for no values as follows: if(document.lion1003.L230.value == "")
{
alert('Please answer question 1');
document.lion1003.L231[0].focus();
return false;
}

This has no result.

So Then I tried to check for "N" or "Y" as follows:
if(document.lion1003.L230.value != "Y" || "N")
{
alert('Please answer question 1');
document.lion1003.L230.focus();
return false;
}

This returns False regardless if the radio button is checked or not.

So what do I need to do to validate a radio button selection of yes or no, vlue="Y" || "N" for type=radio name="L230"?


Thanks

gil davis
08-10-2003, 05:06 PM
When you give form objects identical names, they become an array in JavaScript. You have two radio buttons:
document.lion1003.L230[0].value // would be "Y"
document.lion1003.L230[1].value // would be "N"
You actually have to see if the button is checked:
document.lion1003.L230[0].checked // true if selected
document.lion1003.L230[1].checked // true if selected
You don't get the value in Javascript for free just because it is checked.

SeanPaul72
08-10-2003, 05:34 PM
I changed the validation code to read like this:if(!document.lion1003.L230[0].checked || !document.lion1003.L230[1].checked)

{
alert('Please answer question 1');
document.lion1003.L242.focus();
return false;
}


But it returns false regardless if whether either radio button is checked.

What am I doing wrong?

Thanks

Sean Paul

SeanPaul72
08-10-2003, 05:38 PM
I should be using "and"("&") instead of "or"("||")

thanks for the help

Exuro
08-10-2003, 07:21 PM
Yeah, use "and" (&&) instead. Notice that it is "&&", not just "&". I don't know if you knew that or not, but you only had "&" in your post...

The "or" operator returns true if either value is true. So in your little statement, if either box is unchecked, then it's going to return a true and say that they need to answer a question.

However, if you substituted an "and" in there instead, it would check to be sure that both boxes are unchecked, and if so, then say that they need to answer the question.

Hope that helps!