title: JS Form Validation Fails, Yet Form Still Submits?
body: <p>It's pretty much as the question suggests, I have a HTML form with JS validation. Now, I'm still debugging fine details with the validation, but the problem is that the onSubmit function fires the errors when they should be, but the form continues to submit - I have no idea why. I've check the million and one similar problems on StackOverflow, but none seem to have the same cause as mine - I've checked and check and checked. If anyone could help, I'd greatly appreciate it.</p>
  
  <p>Also, I know my code can be shortened, but I'll do that when I figure this problem out.</p>
  
  <p>Form:</p>
  
  <pre><code>&lt;form name="registerForm" method="post" action="index.php" onSubmit="return validateForm()"&gt;
  
              &lt;table class="formTable" &gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;i class="smallprint"&gt;* denotes a required field.&lt;/i&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;input type="text" maxlength="32" width="32" name="regTxtUsrName"&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      User Name* &lt;i class="smallprint"&gt; &amp;nbsp; between 6 and 32 characters (letters, numbers and underscores only)&lt;/i&gt;&lt;br /&gt;
                      &lt;b class="error" id="userNameError"&gt;Error: Please enter a user name between 6 and 32 characters, using letters, numbers and underscores&lt;/b&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;input type="text" maxlength="32" width="32" name="regTxtFName"&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      First Name*&lt;br /&gt;
                      &lt;b class="error" id="fNameError"&gt;Error: Please enter your first name&lt;/b&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;input type="text" maxlength="32" width="32" name="regTxtSName"&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      Surname*&lt;br /&gt;
                      &lt;b class="error" id="sNameError"&gt;Error: Please enter your surname&lt;/b&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;input type="text" maxlength="32" width="32" name="regTxtEmail"&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      Email* &amp;nbsp; &lt;i class="smallprint"&gt;Please use a valid email address, it will be used to validate your account&lt;/i&gt;&lt;br /&gt;
                      &lt;b class="error" id="emailError1"&gt;Error: Please enter an email address&lt;br /&gt;&lt;/b&gt;
                      &lt;b class="error" id="emailError2"&gt;Error: This is not a valid email address&lt;/b&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;input type="text" maxlength="32" width="32" name="regTxtEmailConf"&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      Confirm Email*&lt;br /&gt;
                      &lt;b class="error" id="emailConfError"&gt;Error: Both email addresses must match&lt;/b&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;input type="password" maxlength="32" width="32" name="regTxtPassword"&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      Password* &amp;nbsp; &lt;i class="smallprint"&gt;Between 6 and 32 characters, using at least one letter and one number&lt;/i&gt;&lt;br /&gt;
                      &lt;b class="error" id="passwordError"&gt;Error: Please enter a valid password&lt;/b&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;input type="password" maxlength="32" width="32" name="regTxtPasswordConf"&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      Confirm Password*&lt;br /&gt;
                      &lt;b class="error" id="passwordConfError"&gt;Error: Both email passwords must match&lt;/b&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;/table&gt;
              &lt;br /&gt;
              &lt;div class="textCenter"&gt;
                  &lt;input type="checkbox" class="center" name="regChkTos"&gt; - Check this box if you agree to the Terms of Service. You must agree in order to regster.
                  &lt;br /&gt;
                  &lt;br /&gt;
                  &lt;input type="submit" name="regSubmit" value="Register"&gt;
              &lt;/div&gt;
  
          &lt;/form&gt;
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      function validateForm()
      {   
          var noError = true;
          if (!validateUserName())
          {
              noError = false;
          }
          if (!validateFName())
          {
              noError = false;
          }
          if (!validateSName())
          {
              noError = false;
          }
          if (!validateEmail())
          {
              noError = false;
          }
          if (!validateConfirmEmail())
          {
              noError = false;
          }
          if (!validatePassword())
          {
              noError = false;
          }
          if (!validateConfirmPassword())
          {
              noError = false;
          }
          return noError;
      }
  
      function validateUserName()
      {
          var userName = document.forms["registerForm"]["regTxtUsrName"];
          var regex = /^\w+$/;
          if (userName.value==null || userName.value=="" || userName.value.length &lt; 6 || !regex.test(userName.value))
          {
              userName.style.border = "2px solid red";
              document.getElementById('userNameError').style.display="inline";
              return false;
          }
          else
          {
              userName.style.border = "2px solid #0f0";
              document.getElementById('userNameError').style.display="none";
              return true;
          }
      }
  
      function validateFName()
      {
          var name = document.forms['registerForm']['regTxtFName'];
          if (name.value == null || name.value == '')
          {
              name.style.border = "2px solid red";
              document.getElementById('fNameError').style.display="inline";
              return false;
          }
          else
          {
              name.style.border = "2px solid #0f0";
              document.getElementById('fNameError').style.display="none";
              return true;
          }
      }
  
      function validateSName()
      {
          var name = document.forms['registerForm']['regTxtSName'];
          if (name.value == null || name.value == '')
          {
              name.style.border = "2px solid red";
              document.getElementById('sNameError').style.display="inline";
              return false;
          }
          else
          {
              name.style.border = "2px solid #0f0";
              document.getElementById('sNameError').style.display="none";
              return true;
          }
      }
  
      function validateEmail()
      {
          noError = true;
          var email = document.forms['registerForm']['regTxtEmail'];
          var atpos=email.value.indexOf("@");
          var dotpos=email.value.lastIndexOf(".");
          if (email.value == null || email.value == '')
          {
              email.style.border = '2px solid red';
              document.getElementById('emailError1').style.display='inline';
              noError = false;
          }
          else
          {
              document.getElementById('emailError1').style.display='none';
              if (atpos&lt;1 || dotpos&lt;atpos+2 || dotpos+2&gt;=email.value.length)
              {
                  email.style.border = '2px solid red';
                  document.getElementById('emailError2').style.display='inline';
                  noError = false;
              }
              else
              {
                  email.style.border = '2px solid #0f0';
                  document.getElementById('emailError2').style.display='none';
              }
          }
          return noError
      }
  
      function validateConfirmEmail()
      {
          var email = document.forms['registerForm']['regTxtEmail'];
          var emailConf = document.forms['registerForm']['regTxtEmailConf'];
          if (email.value != emailConf.value)
          {
              emailConf.style.border = '2px solid red';
              document.getElementById('emailConfError').style.display = 'inline';
              return false
          }
          else
          {
              emailConf.style.border = '2px solid 0f0';
              document.getElementById('emailConfError').style.display = 'none';
              return true
          }
      }
  
      function validatePassword()
      {
          var password = document.forms['registerForm']['regTxtPassword'];
          if (password.value == null || password.value == '' || password.value.length &lt; 6 || password.value.search(/\d/) == -1 || password.value.search(/[A-z]/) == -1)
          {
              password.style.border = '2px solid red';
              document.getElementById('passwordError').style.display = 'inline';
              return false;
          }
          else
          {
              password.style.border = '2px solid 0f0';
              document.getElementById('passwordError').style.display = 'none';
              return true;
          }
      }
  
      function validatePasswordConf()
      {
          var password = document.forms['registerForm']['regTxtPassword'];
          var passwordConf = document.forms['registerForm']['regTxtPasswordConf'];
          if (password.value != passwordConf.value)
          {
              passwordConf.style.border = '2px solid red';
              document.getElementById('passwordConfError').style.display = 'inline';
              return false;
          }
          else
          {
              passwordConf.style.border = '2px solid 0f0';
              document.getElementById('passwordConfError').style.display = 'none';
              return true;
          }
      }
      &lt;/script&gt;
  </code></pre>
  
  <p>Yes, this script is on the HTML page where the form is located. I've left every field blank, clicked submit, the errors pop up temporarily, then <code>action="index.php"</code> is invoked anyway.</p>
  
  <p>Thanks for any help at all.</p>