Solved

How to validate radio button

Posted on 2009-04-15
5
10,743 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 100 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 400 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: 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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

830 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