title: javascript validate multiple radio selections
body: <p>I realize this isn't a new topic and that it has been asked before. I have read through various answers and, although I am getting better at Javascript, am confused by most of the responses.</p>
  
  <p>I am asking my users to give a rating, where they rate both a category and company. I want Javascript to validate that both sets of radio buttons have an answer marked, and if possible, give separate alerts depending on the one that isn't marked.</p>
  
  <p>My code (omitting the tables they are wrapped in)</p>
  
  <pre><code>&lt;form action="blah.php" method="post" onsubmit="return validaterating()"
  name="ratings"&gt;
  
  &lt;input type="radio" name="categoryrate" value="5"&gt;
  &lt;input type="radio" name="categoryrate" value="4"&gt;
  &lt;input type="radio" name="categoryrate" value="3"&gt;
  &lt;input type="radio" name="categoryrate" value="2"&gt;
  &lt;input type="radio" name="categoryrate" value="1"&gt;
  
  &lt;input type="radio" name="companyrate" value="5"&gt;
  &lt;input type="radio" name="companyrate" value="4"&gt;
  &lt;input type="radio" name="companyrate" value="3"&gt;
  &lt;input type="radio" name="companyrate" value="2"&gt;
  &lt;input type="radio" name="companyrate" value="1"&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>function validaterating() {
  
  var rate1 = document.forms.ratings.categoryrate;
  var rate2 = document.forms.ratings.companyrate;
  
  function validaterating() {
  for (var i=0; i &lt; rate1.length; i++) {
  if (rate1[i].checked)
  return true;
  }
  alert("Please rate the category.");
  return false;
  }
  </code></pre>
  
  <p>Now, I know this works to validate the first rating as I have it and is the validation setup that I think makes the most sense to me. However, I can't get the second question (rate2) to validate. I've tried another for loop, using the &amp;&amp; operator, and it stops working when I try to get the second to validate. Using the code as I'm writing it, how is that possible?</p>
  
  <p>Thanks for your time.</p>