Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9231987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:12:21+00:00 2026-06-18T06:12:21+00:00

what is wrong with my code? I need to validate a radio button. it

  • 0

what is wrong with my code? I need to validate a radio button.
it works fine until the last item. after I select Yes or No, I get to submit the form with the last option empty. I cant seem to validate the last option.
also I didnt write this script. I honestly dont understand how it works :/

this is what I have

function valida () {


if (document.example.name.value=="") {
alert ('NAME field is empty!');
return false;
}


else if (document.example.email.value=="") {
alert ('EMAIL field is empty!');
return false;
}



    var radios = document.getElementsByName("yesorno");
    var formValid = false;

    var i = 0;
    while (!formValid && i < radios.length) {
        if (radios[i].checked) formValid = true;
        i++;        
    }

    if (!formValid) alert("Select Yes or No!");
    return formValid;




    var radios = document.getElementsByName("color");
    var formValid = false;

    var i = 0;
    while (!formValid && i < radios.length) {
        if (radios[i].checked) formValid = true;
        i++;        
    }

    if (!formValid) alert("Pick a colour!");
    return formValid;



else {
return true;
}
}










<form name="example" id="example" action="submit-form.php" method="post" onsubmit="return valida();">    
<fieldset>


<label for="name">Name:</label>
<input type="text" name="name" id="name"/>




<label for="email">Email:</label>
<input type="text" name="email" id="email"/>    



<label>Yes or No?</label>    

YES
<input name="yesorno" type="radio" class="radio" value="yes" />

NO
<input name="yesorno" type="radio" class="radio" value="yes" />



<label>Red, green or yellow?</label>    

red
<input name="color" type="radio" class="radio" value="red" />

green
<input name="color" type="radio" class="radio" value="green" />

yellow
<input name="color" type="radio" class="radio" value="yellow" />



<input type="submit" value="submit" />


</fieldset>
</form>

edit:
I found a solution here on stackoverflow. this works for me now:

function valida () {


if (document.example.name.value=="") {
alert ('NAME field is empty!');
return false;
}


else if (document.example.email.value=="") {
alert ('EMAIL field is empty!');
return false;
}



else if ($('input[name=yesorno]:checked').length == 0) {
alert ('Select Yes or No!');
return false;
}


else if ($('input[name=color]:checked').length == 0) {
alert ('Pick a colour!');
return false;
}



else {
return true;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:12:22+00:00Added an answer on June 18, 2026 at 6:12 am

    You’re returning the “Yes/No” validation before the rest of the code can be reached. If the return is enclosed in the if statement, the rest of the code will be reached. Try this:

    <script language=javascript>
    function valida ()
    {
    
    
    if (document.example.name.value=="")
    {
    alert ('NAME field is empty!');
    return false;
    }
    
    
    else if (document.example.email.value=="")
    {
        alert ('EMAIL field is empty!');
        return false;
     }
    
    
    
    var radios = document.getElementsByName("yesorno");
    var formValid = false;
    
    var i = 0;
    while (!formValid && i < radios.length)
    {
        if (radios[i].checked) formValid = true;
        i++;        
    }
    
    if (!formValid)
    {
        alert("Select Yes or No!");
        return formValid;
    }
    
    
    
    
    radios = document.getElementsByName("color");
    formValid = false;
    
    i = 0;
    
    while (!formValid && i < radios.length)
    {
        if (radios[i].checked)
        {
            formValid = true;
        }
        i++;        
    }
    
    if (!formValid)
    {
    alert("Pick a colour!");
    return formValid;
    }
    
    
    else
    {
    return true;
    }
    

    }

    Name:

    Email:

    Yes or No?

    YES

    NO

    Red, green or yellow?

    red

    green

    yellow

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's wrong with the code of radio button? When I've tried SELECT/OPTION it's working.
After validate I need to trigger custom submits on different hidden buttons. Code works
Am I doing something wrong if I need code like this in a Controller?
I need sample javascript code to validate the date with mm-dd-yyyy format. 1) if
I have the following piece of TCL code: #wrong format: set in_val 12 0
The following code produces wrong and inconsistent output with gcc (4.1.2 20080704) but correct
Whats wrong with this code? -(void) drawRect:(CGRect) rect { CGContextRef c = UIGraphicsGetCurrentContext(); if
what's wrong with this code in Google Chrome r = webkitIndexedDB.open(db_name,1); r.onsuccess = success_callback;
What's wrong with this code?: SolverContext sc = SolverContext.GetContext(); Model m = sc.CreateModel(); m.AddDecision(new
what's wrong with my code? i want spinner indicator start when view1.nib start to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.