title: Multiple Validation checks - logic error
body: <p>I am providing a validation feature on a form for passwords. I need to be able to implement a few validation rules and have them all checked on submit. Now to me the code is sound but I think they may be some logic error in my code that I'm too tired to notice (too the coffee machine!)</p>
  
  <p>Here's the JavaScript:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  &lt;!--
      function validate(registerForm)
      registerForm.onsubmit=function()
      {
          var pw1 = document.forms["register"]["password1"].value;
          var pw2 = document.forms["register"]["password2"].value;
  
          //Check values are present in both fields
          if(pw1 == '' || pw2 == '')
          {
              alert("Please enter your password twice.");
              return false;
          }
          //Check there no spaces
          else if(document.forms["register"]["password1"].value.indexOf(invalid) &gt; - 1) 
          {
              alert("Spaces are not allowed in passwords!");
              return false;
          }
          //Check passwords are the same
          else
          {
              if(pw1 != pw2) 
              {
                  alert("The passwords you entered were not the same. Please try again!");
                  return false;
              }
              //Accept passwords
              {
                  alert("Password accepted!");
                  return true;
              }
          }
      }
  --&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p>And the HTML Form to go with it:</p>
  
  <pre><code>&lt;form id="register"&gt;
      &lt;label for="username"&gt;Username&lt;/label&gt;
      &lt;input type="text" class="input_text" name="username" id="name" placeholder="e.g. AberLibrary01" /&gt;
      &lt;br /&gt;
      &lt;label for="password"&gt;Password&lt;/label&gt; 
      &lt;input type="text" class="input_text" name="password1" id="password1" placeholder="e.g. aber01" /&gt;
      &lt;br /&gt;
      &lt;label for="re-enterpassword"&gt;Re-enter password&lt;/label&gt;
      &lt;input type="text" class="input_text" name="password2" id="password2" placeholder="e.g. aber01" /&gt;
  
      &lt;input type="submit" class="button" value="Register" /&gt;
      &lt;/form&gt;
  
      &lt;script type="text/javascript"&gt;
      &lt;!--
          new validate(document.forms['register']);
      --&gt;
      &lt;/script&gt;
  </code></pre>
  
  <p>Any ideas of lovely StackOverflow community? The exact problem is that it won't check for spaces in passwords or whether two passwords entered are the same. It successfully checks that there is at least something in both password fields.</p>
  
  <p>Thanks Dan</p>