title: jquery validation loop producing false positives
body: <pre><code>$(document).ready( function()
  {
  // hides the story and error text when the page loads
  $('.errorText').hide();
  $("#story").hide();
  
  // global variables for the blanks and the textarea forms
  var input = $("form").children();
  var storyBlank = $('#story').children();
  
  // Main Event on Click
  $('button.submit').on( "click", function (event)
  {
      // if the form is not validated, highlights errors and prevents the submit from going through
      if(!validate())
      {
          event.preventDefault();
      }
      // if the form is validated, fills the blanks in the story and displays it
      else
      {
          fillInTheBlanks();
      }
  });
  
  // Checks to see if there are any empty fields and highlights them if they are empty
  function validate()
  {
      console.log('validate() initiated')
      var success = false;
      errcnt = 0;
      cnt = 0;
  
      while (cnt &lt; 9)
      {
          if (input.eq(cnt).val().length == 0)
          {
              errcnt++;
              input.eq(cnt).removeClass("hide");
              console.log('errorcount', errcnt, 'at input', cnt);
          }
          else if (input.eq(cnt).val().length !== 0 &amp;&amp; !(input.eq(cnt)).hasClass("hide"))
          {
              input.eq(cnt).addClass("hide");
          }
          cnt++;
      }
  
      if (errcnt == 0)
      {
          success = true;
      }
      return success;
  }
  
  // Fills in the blanks of the story
  function fillInTheBlanks()
  {
      console.log('fillInTheBlanks() executed');
      var blankCount = 0;
      while (blankCount &lt; 9)
      {
          storyBlank.eq(blankCount).empty().append(input.eq(blankCount).val());
          blankCount++;
      }
      $("#story").show();
  }
  });
  </code></pre>
  
  <p>I am trying to make a mad libs style page with 9 textboxes for input. I am running into two problems. </p>
  
  <p>First, when I click submit with all textboxes empty, only the the first four show an error (this is done in css, I have two classes on all the textboxes "error hide", I remove the class hide in my loop to show the error).</p>
  
  <p>The second problem I'm having is if I click submit with text in all the textboxes, my validate functions errorcount goes up to 4 errors at every other textbox. I've even tried '$('input').eq(0).val().length == 0' for every textbox in the index and it's returning false every time. I don't understand how it's getting into that if then statement if it doesn't satisfy the argument.</p>