I have been struggling for a while to get javascript to validate a WordPress based HTML form which uses radio buttons. I finally came up with a solution with was a bit long-winded but worked – at least in IE and Chrome – however, it doesn’t work in Firefox (which suggests my code is a bit sloppy). I think my radio button reference is the issue. Can anyone help with what I have done wrong – apart from use an inefficient approach for validation :-)?
A simplified version of my form:
<script>
function validateForm()
{
var aa=document.forms["personalise"]["motivation"]["1a"];
var ab=document.forms["personalise"]["motivation"]["1b"];
var ac=document.forms["personalise"]["motivation"]["1c"];
var ad=document.forms["personalise"]["motivation"]["1d"];
var ae=document.forms["personalise"]["motivation"]["1e"];
if (!(aa.checked == true || ab.checked == true || ac.checked == true || ad.checked == true || ae.checked == true))
{
alert("Question 1 must be completed");
return false;
}
}
</script>
<form name="personalise" action="insertdatatest.php" onsubmit="return validateForm()" method="post">
1. Are you seriously planning to quit </b>:
<input id="1a" type="Radio" title="" name="motivation" value="1" /> Within the next 2 weeks
<input id="1b" type="Radio" title="" name="motivation" value="2" /> Within the next 30 days
<input id="1c" type="Radio" title="" name="motivation" value="3" /> Within the next 3 months
<input id="1d" type="Radio" title="" name="motivation" value="4" /> No, I am not currently planning to quit
<input id="1e" type="Radio" title="" name="motivation" value="5" /> I have already quit
<input type="submit" value = "Submit">
</form>
I am a real newbie at web development, so any help would be much appreciated.
Thanks again TheDeadMedic for your help. Actually I found a slightly different way of doing it which worked in all three browsers which was set up for multiple radio button questions (hence the blOK entries). In case it is useful to any others, the code is below. Flix.