[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to validate radio button

Posted on 2009-04-15
5
Medium Priority
?
10,762 Views
Last Modified: 2012-05-06
I have four radio buttons grouped togather as 'vehicle' what I want to do is when a user clicks on submit, I want my JQuery to check if the user has atleast chosen one of the radio buttons if not, I want them to get an alert message .

I have tried the following code which did not work, as it is always returning false;

Any help would be highly appriciated.
jQuery('input[@name="radio"]').each(function()
{
   if( ! $('input:radio:selected', this).length > 0)
   {
	nextMove  = false;
   }
});
if(!nextMove )
  alert('Please choose a vehicle');

Open in new window

0
Comment
Question by:aniga42
  • 3
5 Comments
 
LVL 6

Expert Comment

by:twocandles
ID: 24147064
Since you cannot select more than 1 radio button, that function always returns false because there will be always one radio not selected.

Change the logic a bit: if any of the radios is selected, then return true and exit the function, so the next radio (unselected) won't toggle the flag to false.
0
 

Author Comment

by:aniga42
ID: 24147547
Thanks for such a quick response, do you mind showing me how that code would work? I tried you suggestion (though I am not sure my syntax was correct) still returned false.
Again thanks for your help
0
 
LVL 6

Expert Comment

by:twocandles
ID: 24147669
Sure, could you post more code? I would need the full script and how the form and radio buttons are defined.
0
 
LVL 17

Assisted Solution

by:nplib
nplib earned 400 total points
ID: 24149506
don't know jQuery but this kinda logic states, nextMove is already false, so if the "if statement" returns true then nextMove becomes true amd can't be changed back to false
nextMove  = false;
jQuery('input[@name="radio"]').each(function()
{
   if(($('input:radio:selected', this).length > 0) && (nextMove == false))
   {
        nextMove  = true;
   }
});
if(!nextMove )
  alert('Please choose a vehicle');

Open in new window

0
 
LVL 6

Accepted Solution

by:
twocandles earned 1600 total points
ID: 24150059
Yes, that's the idea.

You can rewrite it like this:

nextMove  = false;
jQuery('input[@name="radio"]').each(function()
{
   // You can remove the second condition because on true you'll exit
   // the loop
   if(($('input:radio:selected', this).length > 0))
   {
        nextMove  = true;
        // You don't need to check for more radios
        return;
   }
});
if(!nextMove )
  alert('Please choose a vehicle');

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web 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 how to count occurrences of each item in an array.
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 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