Solved

Select one javascript for radio buttons

Posted on 2016-08-14
3
65 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
  • 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 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