Solved

How to validate radio button

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

717 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