aniga42
asked on
How to validate radio button
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.
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');
ASKER
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
Again thanks for your help
Sure, could you post more code? I would need the full script and how the form and radio buttons are defined.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.