Solved

Select one javascript for radio buttons

Posted on 2016-08-14
3
74 Views
Last Modified: 2016-08-14
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
0
Comment
Question by:amucinobluedot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 41755471
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

0
 

Author Comment

by:amucinobluedot
ID: 41755498
Can I apply it to onsubmit for the form instead ?
0
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 500 total points
ID: 41755510
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" onsubmit="return oneCheckboxChecked(this)">
<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'))">--> 
    
    <input type="submit" name="submit" value="Lets Submit"/> 
    <br>
</form>

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question