I have JavaScript which checks for text fields and check boxes fine but when comes to radio buttons it fails to distinguish if one of the options was selected.
How to do this correctly?
This is what I have:
validateBugForm = function()
{
var name = document.forms["bug_form"]["Name"].value;
var email = document.forms["bug_form"]["Email"].value;
var tos = document.forms["bug_form"]["Accept"].checked;
var project = document.forms["bug_form"]["project"].value;
if (name == "")
{
alert("You must provide your name or nick name!");
return false;
}
if (project == "")
{
alert("You must choose a project.");
return false;
}
if (email.indexOf ('@', 0) == -1 || email.indexOf ('.', 0) == -1)
{
alert("You must provide your email address!");
return false;
}
if (!tos)
{
alert("You must agree to our Terms of Services.");
return false;
}
}
In my form I have:
<tr>
<td width="30%">Choose a project in which you encountered a problem.
<big><font color="red">*</font></big></td></td>
<td width="10px" class="table_td_space"></td>
<td width="70%">
<input type="radio" name="project" value="1" id="1">1<br>
<input type="radio" name="project" value="2" id="2">2<br>
<input type="radio" name="project" value="3" id="3">3<br>
<input type="radio" name="project" value="4" id="4">4<br>
</td>
</tr>
You need to loop through all of the radio button options to see if one was selected:
http://homepage.ntlworld.com/kayseycarvey/jss3p11.html