Solved

How to validate radio button

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now