Solved

How to validate radio button

Posted on 2009-04-15
5
10,741 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

803 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