title: JavaScript Validation - about the Confirm Password field
body: <p>I am making a sign up page and I am instructed to add client side validation and javascript validation. In the HTML file(signup.html), I have 8 input fields, 7 of those have regular expression, but only 4 have required field. I am having a problem about the "Confirm Password" field. Even though the input is different from the "Password" field, the validation process still continues, instead of alerting the user that the password must be the same to proceed.</p>
  
  <p>Here is the HTML code for the form:</p>
  
  <pre><code>            &lt;form method="POST" action="http://webdevfoundations.net/scripts/formdemo.asp" id="signUp" onSubmit="confirmForm(this);"&gt;
                  &lt;fieldset id="personalInfo"&gt;
                      &lt;legend&gt;
                          Personal Information
                      &lt;/legend&gt;
                      &lt;table class="userInput"&gt;
                          &lt;tr&gt;
                              &lt;td class="label"&gt;
                                  &lt;label for="firstName"&gt;
                                      First Name 
                                  &lt;/label&gt;
                              &lt;/td&gt;
                              &lt;td&gt;
                                  &lt;input type="text" name="first" id="firstName" pattern="[a-zA-Z]{2,}" title="Must have at least 2 characters of alphabet" maxlength="50" autofocus/&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td class="label"&gt;
                                  &lt;label for="lastName"&gt;
                                      Last Name
                                  &lt;/label&gt;
                              &lt;/td&gt;
                              &lt;td&gt;
                                  &lt;input type="text" name="last" id="lastName" pattern="[a-zA-Z]{2,}" title="Must have at least 2 characters of alphabet" maxlength="50"/&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td class="label"&gt;
                                  &lt;label for="email"&gt;
                                      Email&lt;span class="asterisk"&gt;*&lt;/span&gt;
                                  &lt;/label&gt;
                              &lt;/td&gt;
                              &lt;td&gt;
                                  &lt;input type="email" name="email" id="email" pattern="\w.*+\@+[a-z]+\.[a-z]{2,7}" title="Example: abc@yahoo.com" required/&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td class="label"&gt;
                                  &lt;label for="age"&gt;
                                      Age
                                  &lt;/label&gt;
                              &lt;/td&gt;
                              &lt;td&gt;
                                  &lt;input type="number" name="age" id="age" pattern="[1-9]{1,2}" title="Numeric value must be used" min="3"/&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                      &lt;/table&gt;
                  &lt;/fieldset&gt; 
  
                  &lt;br/&gt;
  
                  &lt;fieldset id="genderField"&gt;
                      &lt;legend&gt;
                          Gender
                      &lt;/legend&gt;
                      &lt;input type="radio" name="gender" id="genderM" value="Male"&gt;
                      &lt;label for="genderM"&gt;
                          Male
                      &lt;/label&gt;
  
                      &lt;input type="radio" name="gender" id="genderF" value="Female"&gt;
                      &lt;label for="genderF"&gt;
                          Female
                      &lt;/label&gt;                                
                  &lt;/fieldset&gt;
  
                  &lt;br/&gt;
  
                  &lt;fieldset id="logInfo"&gt; 
                      &lt;legend&gt;
                          Log In Information
                      &lt;/legend&gt;
                      &lt;table class="userInput"&gt;
                          &lt;tr&gt;
                              &lt;td&gt;
                                  &lt;label for="userName"&gt;
                                      Username&lt;span class="asterisk"&gt;*&lt;/span&gt;
                                  &lt;/label&gt;
                              &lt;/td&gt;
                              &lt;td&gt;
                                  &lt;input type="text" name="user" id="userName" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{5,}" title="Must have at least 5 characters, including UPPER/lowercase letters and number" required/&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td&gt;
                                  &lt;label for="password"&gt;
                                      Password&lt;span class="asterisk"&gt;*&lt;/span&gt;
                                  &lt;/label&gt;
                              &lt;/td&gt;
                              &lt;td&gt;
                                  &lt;input type="password" name="pword" id="password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{5,}" title="Must have at least 5 characters, including UPPER/lowercase letters and number" required/&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td&gt;
                                  &lt;label for="confirmP"&gt;
                                      Confirm Password&lt;span class="asterisk"&gt;*&lt;/span&gt;
                                  &lt;/label&gt;
                              &lt;/td&gt;
                              &lt;td&gt;
                                  &lt;input type="password" name="pwordConfirm" id="confirmP" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{5,}" title="Must be the same password as above" required/&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                      &lt;/table&gt;
                  &lt;/fieldset&gt;
  
                  &lt;br /&gt;
                  &lt;input type="submit" value="Sign Up"/&gt;
              &lt;/form&gt;
  </code></pre>
  
  <p>Here is the JavaScript code:</p>
  
  <pre><code>function confirmForm(form)
  {
          if(!(form.first.value.match(/^[a-zA-Z]{2,}$/))) {
          alert("Invalid First Name");
          form.first.focus();
              return false;
      }
  
      if(!(form.last.value.match(/^[a-zA-Z]{2,}$/))) {
          alert("Invalid Last Name");
          form.last.focus();
          return false;
      }
  
      if(!(form.email.value.match(/^\w.*+@[a-z]+\.[a-z]{2,7}$/))) {
          alert("Invalid Email");
          form.email.focus();
          return false;
      }
  
      if(!(form.age.value.match(/^[1-9]{1,3}$/))){
          alert("Invalid Age");
          form.age.focus
          return false
      }
  
      if(!(form.user.value.match(/^\w{5,}$/))) {
          alert("Invalid Username");
          form.user.focus();
          return false;
      }
  
      if(!(form.pword.value.match(/^.\w{5,}$/))){
          alert("Invalid Password");
          form.pword.focus();
          return false;
      }
  
      if(!(form.pwordConfirm.value == form.pword.value)){
          alert("Password must be the same");
          form.pwordConfirm.focus();
          return false;
      }
      else {
          return true;
      }
  }
  </code></pre>