title: Javascript form submits even though it is not valid
body: <p>I am having quite a few issues using java script to validate an application form.</p>
  
  <p>The way my code is now, it still allows me to submit my form even though there are errors.
  The method I am trying to use is to make each function return false when it hits an error but it is not working and I am unsure why.</p>
  
  <p><b>FIXED</b>, thank you!</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>"use strict"; 
  var errMsg = "";
  /*get variables from form and check rules*/
  function validate(){
  var result = true; 
  
  result = validateDOB() &amp;&amp; validatePostCode() &amp;&amp; validateTextBox() &amp;&amp; validateJobReferenceNumber();
  
  if (errMsg != ""){ //only display message box if there is something to show
   alert(errMsg);
   }
  
  return result;
  }
  
  function validateDOB(){
  var dob = document.forms["regForm"]["dob"].value;
  var age = getAge(dob);
  // var error = document.getElementById("spandob");
  var pattern =/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;
   if (dob == null || dob == "" || !pattern.test(dob) || age &lt; 15 || age &gt; 80) {
          errMsg += "Please enter a valid date of birth\n";
          return false;
      }
      else {
          return true;
      }
  }
  
  function getAge(dob) { /* find age from today's date minus entered DOB*/
      var today = new Date();
      var birthDate = new Date(dob);
      var age = today.getFullYear() - birthDate.getFullYear();
      var m = today.getMonth() - birthDate.getMonth();
      if (m &lt; 0 || (m === 0 &amp;&amp; today.getDate() &lt; birthDate.getDate())) {
          age--;
      }
      return age;
  }
  
  function validatePostCode(){
   var postcode = document.forms["regForm"]["postcode"].value;
   var state = document.forms["regForm"]["state"].value; 
   var error = document.getElementById("spanPostcode");
   switch (state){
    case "VIC":
    if (postcode.charAt(0) !== 3 || postcode.charAt(0) !== 8){
     error.innerhtml = "Please enter a valid post code for VIC";
     return false;
    }
    break;
    case "NSW":
    if (postcode.charAt(0) !== 1 || postcode.charAt(0) !== 2){
     errMsg += "Please enter a valid post code for NSW"
     return false;
    }
    break;
    case "QLD":
    if (postcode.charAt(0) !== 4 || postcode.charAt(0) !== 9){
     errMsg += "Please enter a valid post code for QLD"
     return false;
    }
    break;
    case "NT":
    if (postcode.charAt(0) !== 0){
     errMsg += "Please enter a valid post code for NT"
     return false;
    }
    break;
    case "WA":
    if (postcode.charAt(0) !== 6){
     errMsg += "Please enter a valid post code for WA"
     return false;
    }
    break;
    case "SA":
    if (postcode.charAt(0) !== 5){
     errMsg += "Please enter a valid post code for SA"
     return false;
    }
    break;
    case "TAS":
    if (postcode.charAt(0) !== 7){
     errMsg += "Please enter a valid post code for TAS"
     return false;
    }
    break;
    case "ACT":
    if (postcode.charAt(0) !== 0) {
     errMsg += "Please enter a valid post code for ACT"
     return false;
    }
    break;
    default: 
    errMsg += "Please enter a valid post code";
    return false;
   }
   return true;
  }
  
  function validateTextBox(){
   var checkboxOtherSkills = document.getElementById("skills[]").getElementsByTagName("input"); 
   var textbox = document.forms["regForm"]["otherSkills"].value;
  
   if (checkboxOtherSkills[oSkills].checked){
    if (textbox.length &lt; 1){
     errMsg +="'Other Skills' has been selected but not filled out.";
     return false;
    }
   } 
   return true;
  }
  
  function validateJobReferenceNumber(){
   var refnumb = document.getElementById("jobrefno");
   var pattern = (/([a-zA-Z0-9_-]){6}$/);
  
   if (!pattern.test(refnumb)){
    errMsg += "6 alphanumeric characters only"
    return false;
   }
   return true;
  }
  
  function init(){
   var regForm = document.getElementById("regForm");// get ref to the HTML element
   regForm.onsubmit = validate; 
  }
  
  window.onload = init;</code></pre>
  </div>
  </div>
  </p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;form id="regForm" method="post" action="http://mercury.ict.swin.edu.au/it000000/formtest.php" novalidate="novalidate"&gt;
      &lt;fieldset&gt;&lt;legend&gt;Application:&lt;/legend&gt;
  
      &lt;!--Job Reference Number--&gt;
      &lt;p&gt;&lt;label for="jobrefno"&gt;Job Reference Number&lt;/label&gt;
      &lt;input type="text" name="jobrefno" id="jobrefno" pattern="[a-zA-Z0-9]{6}" title="Must be 6 characters" required="required" /&gt;&lt;/p&gt;
      &lt;!--First Name--&gt;
      &lt;p&gt;&lt;label for="firstName"&gt;First Name&lt;/label&gt;
      &lt;input type="text" name="firstName" id="firstName" pattern="[a-zA-Z]+${1,25}" maxlength="25" title="Max Length 25" required="required" /&gt;&lt;/p&gt;
      &lt;!--Last Name--&gt;
      &lt;p&gt;&lt;label for="lastName"&gt;Last Name&lt;/label&gt;
      &lt;input type="text" name="lastName" id="lastName" pattern="[a-zA-Z ]+${1,25}" maxlength="25" title="Max Length 25" required="required" /&gt;&lt;/p&gt;
      &lt;!--Date of Birth--&gt;
          &lt;p&gt;&lt;label for="dob"&gt;Date of Birth&lt;/label&gt;
      &lt;input type="text" name="dob" id="dob" required="required" placeholder="dd/mm/yyyy"/&gt;&lt;/p&gt;
      &lt;!--Gender--&gt;
      &lt;fieldset&gt;&lt;legend&gt;Gender&lt;/legend&gt;
      &lt;p&gt;&lt;label&gt;  &lt;input type="radio" name="gender" value="Unspecified" checked="checked"/&gt;Prefer not to say&lt;/label&gt;
      &lt;label&gt;&lt;input type="radio" name="gender" value="Female" /&gt;Female&lt;/label&gt;
      &lt;label&gt;&lt;input type="radio" name="gender" value="Male" /&gt;Male&lt;/label&gt;&lt;/p&gt;&lt;/fieldset&gt; 
      &lt;!--Street Address--&gt;
      &lt;p&gt;&lt;label for="streetAddress"&gt;Street Address&lt;/label&gt;
      &lt;input type="text" name="streetAddress" id="streetAddress" maxlength="40" title="Max Length 40" required="required" /&gt;&lt;/p&gt;
      &lt;!--Suburb--&gt;
      &lt;p&gt;&lt;label for="suburb"&gt;Suburb&lt;/label&gt;
      &lt;input type="text" name="suburb" id="suburb" maxlength="40" title="Max Length 40" required="required" /&gt;&lt;/p&gt;
      &lt;!--State--&gt;    
      &lt;p&gt;&lt;label for="state"&gt;State&lt;/label&gt;
      &lt;select required ="required" name="state" id="state"&gt;
      &lt;option value="" selected="selected"&gt;Please Select&lt;/option&gt;
      &lt;option value="VIC"&gt;VIC&lt;/option&gt;
      &lt;option value="NSW"&gt;NSW&lt;/option&gt;
      &lt;option value="QLD"&gt;QLD&lt;/option&gt;
      &lt;option value="NT"&gt;NT&lt;/option&gt;
      &lt;option value="WA"&gt;WA&lt;/option&gt;
      &lt;option value="SA"&gt;SA&lt;/option&gt;
      &lt;option value="TAS"&gt;TAS&lt;/option&gt;
      &lt;option value="ACT"&gt;ACT&lt;/option&gt;
      &lt;/select&gt;
      &lt;/p&gt;
      &lt;!--Postcode--&gt;
      &lt;p&gt;&lt;label for="postcode"&gt;PostCode&lt;/label&gt;&lt;span id="spanPostcode"&gt;&lt;/span&gt;
      &lt;input type="text" name="postcode" id="postcode" pattern="[0-9]{4}" maxlength="4" title="4 Digits only" required="required" /&gt;
      &lt;!--Email address--&gt;
      &lt;p&gt;&lt;label for="email"&gt;Email&lt;/label&gt;
      &lt;input type="email" name="email" id="email" placeholder="youremail@host.com" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" title="Please use correct format: eg. mrburns@hotmail.com" required="required" /&gt;&lt;/p&gt;
      &lt;!--Phone Number--&gt;
      &lt;p&gt;&lt;label for="phoneNumber"&gt;Phone Number&lt;/label&gt;
      &lt;input type="text" name="phoneNumber" id="phoneNumber" pattern="[0-9]+{8,12}" maxlength = "12" title="8-12 Digits" required="required" /&gt;       
      &lt;/p&gt;
      &lt;!--Skill List--&gt;
      &lt;label&gt;Skills List&lt;/label&gt;&lt;br&gt;
      &lt;label&gt;&lt;input type="checkbox" name="skills[]" value="HTML5" checked="checked" /&gt;HTML5&lt;/label&gt;&lt;br&gt;
      &lt;label&gt;&lt;input type="checkbox" name="skills[]" value="CSS" /&gt;CSS&lt;/label&gt;&lt;br&gt;
      &lt;label&gt;&lt;input type="checkbox" name="skills[]" value="JavaScript"&gt;JavaScript&lt;/label&gt;&lt;br&gt;
      &lt;label&gt;&lt;input type="checkbox" name="skills[]" value="Excel" /&gt;Excel&lt;/label&gt;&lt;br&gt;
      &lt;label&gt;&lt;input type="checkbox" name="skills[]" value="MySQL" /&gt;MySQL&lt;/label&gt;&lt;br&gt;
      &lt;label&gt;&lt;input type="checkbox" name="skills[]" value="Administration" /&gt;Administration&lt;/label&gt;&lt;br&gt;
      &lt;label&gt;&lt;input type="checkbox" name="skills[]" value="12MoExp" /&gt;12 Months Experience&lt;/label&gt;&lt;br&gt;
      &lt;label&gt;&lt;input type="checkbox" name="skills[]" id ="oSkills" value="OtherSkills" /&gt;Other Skills...&lt;/label&gt;&lt;br&gt;
      &lt;!--Other Skills--&gt;
      &lt;p&gt;&lt;label for="otherSkills"&gt;Other Skills&lt;/label&gt;&lt;br&gt;
      &lt;textarea id="otherSkills" name="otherSkills" rows="10" cols="50" placeholder="Please tell us about any other skills you have that may benefit you in this position"&gt;&lt;/textarea&gt;&lt;/p&gt;
      &lt;/fieldset&gt;
      &lt;p&gt;
      &lt;input type= "submit" value="Apply"/&gt;
      &lt;input type= "reset" value="Reset Form"/&gt;&lt;/p&gt;
  &lt;/form&gt;
  </code></pre>