title: How to check the Radio button validation using javascript in HTML form
body: <p>I Need to validate a radio button input, i.e. when submit button is pressed and no radio buttons have been selected, it alerts the user saying 'please select option', and if a radio button has been selected then simply submit the form and pages redirect to another pages, needs no alert. But in my code, whenever I pressed submit button. It shows the 'please select option' even if all the radio button are selected. Please help.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
   &lt;head&gt;
  
    &lt;style&gt;
     label{display:inline-block;width:100px;margin-bottom:10px;}
    &lt;/style&gt;
      &lt;title&gt;PVQ Questionary&lt;/title&gt;
  
  &lt;!-- =========================================================== --&gt;
  &lt;script LANGUAGE="JavaScript"&gt;
  function validateForm() {
      var radios = document.getElementsByName("yes no");
      var formValid = false;
  
      var i = 0;
      while (!formValid &amp;&amp; i &lt; radios.length) {
          if (radios[i].checked) formValid = true;
          i++;        
      }
  
      if (!formValid) alert("Must check some option!");
      return formValid;
  }
  &lt;/script&gt;
  &lt;!-- =========================================================== --&gt;
  
  
   &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;form name="form1" action="process.php" onsubmit="return validateForm()" method="post"&gt;
  
  &lt;!-- &lt;form method="post" action="process.php"&gt; --&gt;
  
      &lt;label&gt;First Name&lt;/label&gt;
      &lt;input type="text" name="first_name" /&gt;
      &lt;br /&gt;
  
      &lt;label&gt;Last Name&lt;/label&gt;
      &lt;input type="text" name="last_name" /&gt;
      &lt;br /&gt;
  
      &lt;label&gt;Department&lt;/label&gt;
      &lt;input type="text" name="department" /&gt;
      &lt;br /&gt;
  
      &lt;label&gt;Email&lt;/label&gt;
      &lt;input type="text" name="email" /&gt;
      &lt;br /&gt;
  
      &lt;label&gt;Location&lt;/label&gt;
      &lt;input type="text" name="location" /&gt;
      &lt;br /&gt;
  
    &lt;label&gt;Sex&lt;/label&gt;
      &lt;input type="radio" name="sex" value="male" &gt;Male
      &lt;br/&gt;
  
  
  &lt;div align="center"&gt;
  &lt;FONT size="" color="blue"&gt; 
  &lt;h1&gt;Portrait Values Questionnaire (PVQ) for Male &lt;/h1&gt;
  &lt;h2&gt; Here we briefly describe some people. Please read each description and think about how much each person is or is not like you. Put an "Yes" or "No" in the given options that shows how much the person in the description is like you.&lt;/h2&gt;
  &lt;/div&gt;
  &lt;/FONT&gt; 
  
  &lt;h3&gt;1. Thinking up new ideas and being creative is important to him. He likes to do things in his own original way.&lt;/h3&gt;
      &lt;input type="radio" name="answer1" value="yes" &gt;Yes
      &lt;input type="radio" name="answer1" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;2. It is important to him to be rich. He wants to have a lot of money and expensive things.&lt;/h3&gt;
      &lt;input type="radio" name="answer2" value="yes" &gt;Yes
      &lt;input type="radio" name="answer2" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;3. He thinks it is important that every person in the world be treated equally. He believes everyone should have equal opportunities in life.&lt;/h3&gt;
      &lt;input type="radio" name="answer3" value="yes" &gt;Yes
      &lt;input type="radio" name="answer3" value="no"&gt;No
      &lt;br/&gt;
  
  &lt;h3&gt;4. It's very important to him to show his abilities. He wants people to admire what he does.&lt;/h3&gt;
      &lt;input type="radio" name="answer4" value="yes" &gt;Yes
      &lt;input type="radio" name="answer4" value="no"&gt;No
      &lt;br/&gt;
  
  &lt;h3&gt;5.It is important to him to live in secure surroundings. He avoids anything that might endanger his safety.&lt;/h3&gt;
      &lt;input type="radio" name="answer5" value="yes" &gt;Yes
      &lt;input type="radio" name="answer5" value="no"&gt;No
      &lt;br/&gt;
  
  &lt;h3&gt;6.He thinks it is important to do lots of different things in life. He always looks for new things to try.&lt;/h3&gt;
      &lt;input type="radio" name="answer6" value="yes" &gt;Yes
      &lt;input type="radio" name="answer6" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;7.He believes that people should do what they're told. He thinks people should follow rules at all times, even when no-one is watching.&lt;/h3&gt;
      &lt;input type="radio" name="answer7" value="yes" &gt;Yes
      &lt;input type="radio" name="answer7" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;8.It is important to him to listen to people who are different from him. Even when he disagrees with them, he still wants to understand them.&lt;/h3&gt;
      &lt;input type="radio" name="answer8" value="yes" &gt;Yes
      &lt;input type="radio" name="answer8" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;9.He thinks it's important not to ask for more than what you have. He believes that people should be satisfied with what they have.&lt;/h3&gt;
      &lt;input type="radio" name="answer9" value="yes" &gt;Yes
      &lt;input type="radio" name="answer9" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;10.He seeks every chance he can to have fun. It is important to him to do things that give him pleasure.&lt;/h3&gt;
      &lt;input type="radio" name="answer10" value="yes" &gt;Yes
      &lt;input type="radio" name="answer10" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;11.It is important to him to make his own decisions about what he does. He likes to be free to plan and to choose his activities for himself.&lt;/h3&gt;
      &lt;input type="radio" name="answer11" value="yes" &gt;Yes
      &lt;input type="radio" name="answer11" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;12.It's very important to him to help the people around him. He wants to care for their well-being.&lt;/h3&gt;
      &lt;input type="radio" name="answer12" value="yes" &gt;Yes
      &lt;input type="radio" name="answer12" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;13.Being very successful is important to him. He likes to impress other people.&lt;/h3&gt;
      &lt;input type="radio" name="answer13" value="yes" &gt;Yes
      &lt;input type="radio" name="answer13" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;14.It is very important to him that his country be safe. He thinks the state must be on watch against threats from within and without.&lt;/h3&gt;
      &lt;input type="radio" name="answer14" value="yes" &gt;Yes
      &lt;input type="radio" name="answer14" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;15.He likes to take risks. He is always looking for adventures.&lt;/h3&gt;
      &lt;input type="radio" name="answer15" value="yes" &gt;Yes
      &lt;input type="radio" name="answer15" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;16.It is important to him always to behave properly. He wants to avoid doing anything people would say is wrong.&lt;/h3&gt;
      &lt;input type="radio" name="answer16" value="yes" &gt;Yes
      &lt;input type="radio" name="answer16" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;17.It is important to him to be in charge and tell others what to do. He wants people to do what he says.&lt;/h3&gt;
      &lt;input type="radio" name="answer17" value="yes" &gt;Yes
      &lt;input type="radio" name="answer17" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;18.It is important to him to be loyal to his friends. He wants to devote himself to people close to him.&lt;/h3&gt;
      &lt;input type="radio" name="answer18" value="yes" &gt;Yes
      &lt;input type="radio" name="answer18" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;19.He strongly believes that people should care for nature. Looking after the environment is important to him.&lt;/h3&gt;
      &lt;input type="radio" name="answer19" value="yes" &gt;Yes
      &lt;input type="radio" name="answer19" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;20.Religious belief is important to him. He tries hard to do what his religion requires.&lt;/h3&gt;
      &lt;input type="radio" name="answer20" value="yes" &gt;Yes
      &lt;input type="radio" name="answer20" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;21.It is important to him that things be organized and clean. He really does not like things to be a mess.&lt;/h3&gt;
      &lt;input type="radio" name="answer21" value="yes" &gt;Yes
      &lt;input type="radio" name="answer21" value="no"&gt;No
      &lt;br/&gt;
  
  &lt;h3&gt;22. He thinks it's important to be interested in things. He likes to be curious and to try to understand all sorts of things.&lt;/h3&gt;
      &lt;input type="radio" name="answer22" value="yes" &gt;Yes
      &lt;input type="radio" name="answer22" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;23. He believes all the worlds’ people should live in harmony. Promoting peace among all groups in the world is important to him.&lt;/h3&gt;
      &lt;input type="radio" name="answer23" value="yes" &gt;Yes
      &lt;input type="radio" name="answer23" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;24.He thinks it is important to be ambitious. He wants to show how capable he is.&lt;/h3&gt;
      &lt;input type="radio" name="answer24" value="yes" &gt;Yes
      &lt;input type="radio" name="answer24" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;25.He thinks it is best to do things in traditional ways. It is important to him to keep up the customs he has learned.&lt;/h3&gt;
      &lt;input type="radio" name="answer25" value="yes" &gt;Yes
      &lt;input type="radio" name="answer25" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;26.Enjoying life's pleasures is important to him. He likes to 'spoil' himself.&lt;/h3&gt;
      &lt;input type="radio" name="answer26" value="yes" &gt;Yes
      &lt;input type="radio" name="answer26" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;27.It is important to him to respond to the needs of others. He tries to support those he knows.&lt;/h3&gt;
      &lt;input type="radio" name="answer27" value="yes" &gt;Yes
      &lt;input type="radio" name="answer27" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;28.He believes he should always show respect to his parents and to older people. It is important to him to be obedient.&lt;/h3&gt;
      &lt;input type="radio" name="answer28" value="yes" &gt;Yes
      &lt;input type="radio" name="answer28" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;29.He wants everyone to be treated justly, even people he doesn't know. It is important to him to protect the weak in society.&lt;/h3&gt;
      &lt;input type="radio" name="answer29" value="yes" &gt;Yes
      &lt;input type="radio" name="answer29" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;30.He likes surprises. It is important to him to have an exciting life.&lt;/h3&gt;
      &lt;input type="radio" name="answer30" value="yes" &gt;Yes
      &lt;input type="radio" name="answer30" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;31.He tries hard to avoid getting sick. Staying healthy is very important to him.&lt;/h3&gt;
      &lt;input type="radio" name="answer31" value="yes" &gt;Yes
      &lt;input type="radio" name="answer31" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;32.Getting ahead in life is important to him. He strives to do better than others.&lt;/h3&gt;
      &lt;input type="radio" name="answer32" value="yes" &gt;Yes
      &lt;input type="radio" name="answer32" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;33.Forgiving people who have hurt him is important to him. He tries to see what is good in them and not to hold a grudge.&lt;/h3&gt;
      &lt;input type="radio" name="answer33" value="yes" &gt;Yes
      &lt;input type="radio" name="answer33" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;34.It is important to him to be independent. He likes to rely on himself.&lt;/h3&gt;
      &lt;input type="radio" name="answer34" value="yes" &gt;Yes
      &lt;input type="radio" name="answer34" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;35.Having a stable government is important to him. He is concerned that the social order be protected.&lt;/h3&gt;
      &lt;input type="radio" name="answer35" value="yes" &gt;Yes
      &lt;input type="radio" name="answer35" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;36.It is important to him to be polite to other people all the time. He tries never to disturb or irritate others.&lt;/h3&gt;
      &lt;input type="radio" name="answer36" value="yes" &gt;Yes
      &lt;input type="radio" name="answer36" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;37.He really wants to enjoy life. Having a good time is very important to him.&lt;/h3&gt;
      &lt;input type="radio" name="answer37" value="yes" &gt;Yes
      &lt;input type="radio" name="answer37" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;38. It is important to him to be humble and modest. He tries not to draw attention to himself.&lt;/h3&gt;
      &lt;input type="radio" name="answer38" value="yes" &gt;Yes
      &lt;input type="radio" name="answer38" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;39. He always wants to be the one who makes the decisions. He likes to be the leader.&lt;/h3&gt;
      &lt;input type="radio" name="answer39" value="yes" &gt;Yes
      &lt;input type="radio" name="answer39" value="no"&gt;No
      &lt;br/&gt;
  
  
  &lt;h3&gt;40. It is important to him to adapt to nature and to fit into it. He believes that people should not change nature.&lt;/h3&gt;
      &lt;input type="radio" name="answer40" value="yes" &gt;Yes
      &lt;input type="radio" name="answer40" value="no"&gt;No
      &lt;br/&gt;
  
  &lt;input type="submit" value="SUBMIT"&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>