title: jQuery Use Loop for Validation?
body: <p>I have rather large form and along with PHP validation (ofc) I would like to use jQuery. I am a novice with jQuery, but after looking around I have some code working well. It is checking the length of a Text Box and will not allow submission if it is under a certain length. If the entry is lower the colour of the text box changes Red. </p>
  
  <p>The problem I have is as the form is so large it is going to take a long time, and a lot of code to validate each and every box. I therefore wondered is there a way I can loop through all my variables rather than creating a function each time. </p>
  
  <p>Here is what I have:</p>
  
  <pre><code>var form = $("#frmReferral");
  var companyname = $("#frm_companyName");
  var companynameInfo = $("#companyNameInfo");
  var hrmanagername = $("#frm_hrManager");
  var hrmanagernameInfo = $("#hrManagerInfo");
  
  form.submit(function(){
      if(validateCompanyName() &amp; validateHrmanagerName())
          return true
      else
          return false;
  });
  </code></pre>
  
  <p>Validation Functions</p>
  
  <pre><code>function validateCompanyName(){
      // NOT valid
      if(companyname.val().length &lt; 4){
          companyname.removeClass("complete");
          companyname.addClass("error");
          companynameInfo.text("Too Short. Please Enter Full Company Name.");
          companynameInfo.removeClass("complete");
          companynameInfo.addClass("error");
          return false;
      }
      //valid
      else{
          companyname.removeClass("error");
          companyname.addClass("complete");
          companynameInfo.text("Valid");
          companynameInfo.removeClass("error");
          companynameInfo.addClass("complete");
          return true;
      }
  }
  
  function validateHrmanagerName(){
      // NOT Valid
      if(hrmanagername.val().length &lt; 4){
          hrmanagername.removeClass("complete");
          hrmanagername.addClass("error");
          hrmanagernameInfo.text("Too Short. Please Enter Full Name.");
          hrmanagernameInfo.removeClass("complete");
          hrmanagernameInfo.addClass("error");
          return false;
      }
      //valid
      else{
          hrmanagername.removeClass("error");
          hrmanagername.addClass("complete");
          hrmanagernameInfo.text("Valid");
          hrmanagernameInfo.removeClass("error");
          hrmanagernameInfo.addClass("complete");
          return true;
      }
  }
  </code></pre>
  
  <p>As you can see for 50+ input boxes this is going to be getting huge. I thought maybe a loop would work but not sure which way to go about it. Possibly Array containing all the variables? Any help would be great.</p>