'Validating Radio Buttons on Submit
I am trying to validate radio buttons on a competition, its multi-step so the radio buttons post the selection over to the entry form page.
Currently having trouble blocking the submission if there is nothing selected, got a script together but its not working.
HTML:
<form name="radioOptions" method="post" action="#" onsubmit="return validateForm()">
<ul>
<li><input type="radio" name="question" id="question1" value="questionL1">Miami</li>
<li><input type="radio" name="question" id="question2" value="questionL2">Washington</li>
<li><input type="radio" name="question" id="question3" value="questionL3">Rome</li>
<li><button class="enterButton" name='submit' type='submit' /></button></li>
</ul>
</form>
Script:
function validateForm() {
var radios = document.getElementsByName("question");
var formValid = false;
var i = 0;
while (!formValid && i < radios.length) {
if (radios[i].checked) formValid = true;
i++;
}
if (!formValid) alert("Must check some option!");
return formValid;
}
Also have it in a fiddle over HERE
Some help with this would be greatly appreciated.
Solution 1:[1]
Not entirely sure if I did anything, but my exact code as I had it is suddenly working.
@Pow-Lan, using "javascript:" in place of return does not block the page from running the post function.
Thanks all the same guys.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |