title: set of radio button validation using Javascript
body: <p>I am trying to validate 2 sets of radio button using Javascript. It is working with one set but not when I add another radio set (secondtime visitor). Here is my code:</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;form name="form1" action="#" method="post"&gt; 
      First time visitor?:&lt;br/&gt;
      &lt;label for="s1"&gt;Yes&lt;/label&gt;
      &lt;input type="radio" id="radio1" name="firsttime" value="1"/&gt;
      &lt;br/&gt;
      &lt;label for="s2"&gt;No&lt;/label&gt;
      &lt;input type="radio" id="radio2" name="firsttime" value="2"/&gt;
  
      &lt;br/&gt;     
  
      Second time visitor?:&lt;br/&gt;
      &lt;label for="s1"&gt;Yes&lt;/label&gt;
      &lt;input type="radio" id="radio1" name="secondTime" value="1"/&gt;
      &lt;br/&gt;
      &lt;label for="s2"&gt;No&lt;/label&gt;
      &lt;input type="radio" id="radio2" name="secondTime" value="2"/&gt;
  
      &lt;br/&gt;     
  
      &lt;button type="submit" name="nextBTN" onclick="return validateForm();"&gt;Next&lt;/button&gt;&lt;br/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And Javascript code:</p>
  
  <pre><code>function validateForm() {
      var radios = document.getElementsByName("firsttime");
      var radios2 = document.getElementsByName("secondtime");
      var formValid = false;
  
      var i = 0;
      while (!formValid &amp;&amp; i &lt; radios.length) {
          if (radios[i].checked) formValid = true;
          i++;        
      }
  
      var j = 0;
      while (!formValid &amp;&amp; i &lt; radios2.length) {
          if (radios2[j].checked) formValid = true;
          j++;        
      }
  
  
      if (!formValid) alert("Must check some option!");
      return formValid;
  
  
  
  }
  </code></pre>