Forum Moderators: open
I am trying to make sure the user picks ONE radio button, and does not miss them.
here is the code:
<html>
<head>
<title>Radio button validation</title>
<script type="text/javascript">
<!--
function checkForm(form) {
for (var i=0; i<form.button.length; i++) {
if (form.button.checked == false) {
alert("you have to choose a button");
return false;
}
}
alert("this is not working");
}
//-->
</script>
</head>
<body>
<form method="post" action="#" onsubmit="checkForm(this)">
Larry: <input type="radio" name="button" value="larry"><br>
Moe: <input type="radio" name="button" value="moe"><br>
Curly: <input type="radio" name="button" value="curly"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
I swear I am close?
cheers for help.
s.
You've seen that when you use a radio group by name you get a collection, so you loop through it, but you are then testing directly against the collection itself, not one of it's members:
form.button.checked == false
It doesn't have a checked property, so "undefined" is compared to false - ie: false.
You also need to test to see whther none of them has been checked. You have been testing to see whether any of them is unchecked - which is always the case with radios.
I've referenced via the form's elements collection, which is more 'proper' these days.
function checkForm(form)
{
var checked = false;
var buttons = form.elements.button;
for (var i=0; i<buttons.length; i++)
{
if (buttons[i].checked) {
checked = true;
break;
}
}
if(!checked)
alert("you have to choose a button");
return checked ;
}
You will also have to return a true/false value, and return that within the event handler to prevent the form being submitted if false.
[pre]onsubmit="return checkForm(this)"[/pre]
<HTML>
<Head>
<Script Language=JavaScript>
function checkForm(isForm,btnID){
nRadio = 0;
ok = false;
for (n=0; n<isForm.length; n++)
{
if (isForm[n].type == 'radio'){nRadio++}
}
for (i=0; i<nRadio; i++)
{
if(isForm.button[i].checked == true){ok = true}
}
if (ok == false){alert('Please make a selection')};
else {document.getElementById(btnID).disabled = true}
}
</Script>
</Head>
<body>
<form name='demoForm'>
Larry: <input type="radio" name="button" value="larry"><br>
Moe: <input type="radio" name="button" value="moe"><br>
Curly: <input type="radio" name="button" value="curly"><br>
<input type="button" id='submit1' value="Submit" onClick="checkForm(demoForm,'submit1')">
</form>
</body>
</html>
thanks for the help...
cochrane, yours works nicely on its' own, but I am intending to use this in a validation function that test other fields as well(regular text fields, etc). So I want to chuck this into a large function (checkForm(form))that does other things as well, any way to do that?
Okay, this line:
if (isForm[n].type == 'radio'){nRadio++}
well, you can check for all the different "types". A text area, is 'textarea'. I can't recall of them, but just put an alert, like this:
for (n=0; n<isForm.length; n++){alert(isForm[n].type)}
so that you can find out all the element designations, and then just check for them all.
If this doesn't help, then can you provide an example, and I'll provide an example in response.