Link to home
Start Free TrialLog in
Avatar of Aleks
AleksFlag for United States of America

asked on

Select one javascript for radio buttons

I have the javascript below to make sure a user checks at least one checkbox.
Seems to work fine for checkboxes but not for radio buttons. I need help tweaking it so that it works.

I have a repeated region displaying names, they all have ids, I need the user to select one of the radio buttons.

<script language="JavaScript">
function oneCheckboxChecked(form){
for(f=0;f<form.length;f++){
if(form[f].type !="checkbox") continue;
if(form[f].checked){
return true;
}
}
alert("Select at least one User");{
return false;
}
}
</script>

Open in new window


A
Avatar of Rajar Ahmed
Rajar Ahmed
Flag of India image

Try this,
<script language="JavaScript">
function oneCheckboxChecked(form){
for(f=0;f<form.length;f++){
if(form[f].type =="radio") 
if(form[f].checked){
return true;
}
}
alert("Select at least one User");
return false;
}
</script>

<form id="form1">
<input type="radio" name="gender" value="male" > Male<br>
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="radio" name="gender" value="other"> Other

<input type="button" name="button" value="Click" onclick="oneCheckboxChecked(document.getElementById('form1'))"> <br>
</form>

Open in new window

Avatar of Aleks

ASKER

Can I apply it to onsubmit for the form instead ?
ASKER CERTIFIED SOLUTION
Avatar of Rajar Ahmed
Rajar Ahmed
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial