title: JavaScript Form Validation Not Working onSubmit
body: <p>I'm working through a section of a book on form validation with JavaScript.  Unfortunately, the example in the book isn't working.  I understand and follow the logic behind the validation (The general idea is that if any of the individual field validation functions return a value to the validate function then an alert will display and the form will not be submitted <code>onSubmit</code>) , but nothing is happening onSubmit.  I have looked over the script and html a few times and can't find anything wrong (I even went to the book site and copied the code from there).</p>
  
  <p>Here is the html form and the JavaScript validation:  </p>
  
  <pre><code>    &lt;script&gt;
      function validate(form) 
      {
              fail  = validateForename(form.forename.value)
              fail += validateSurname(form.surname.value)
              fail += validateUsername(form.username.value)
              fail += validatePassword(form.password.value)
              fail += validateAge(form.age.value)
              fail += validateEmail(form.email.value)
              if (fail == "") return true
              else {alert(fail); return false }
      }
      &lt;/script&gt;
  
      &lt;script&gt;
      function validateForename(field) {
              if (field == "") return "No Forename was entered.\n"
              return ""
      }
  
      function validateSurname(field) {
              if (field == "") return "No Surname was entered.\n"
              return ""
      }
  
      function validateUsername(field) {
              if (field == "") return "No Username was entered.\n"
              else if (field.length &lt; 5) return "Usernames must be at least 5 characters.\n"
              else if (/[^a-zA-Z0-9_-]/.test(field)) return "Only a-z, A-Z, 0-9, - and _ allowed in Usernames.\n"
              return ""
      }
  
      function validatePassword(field) {
              if (field == "") return "No Password was entered.\n"
              else if (field.length &lt; 6) return "Passwords must be at least 6 characters.\n"
              else if (! /[a-z]/.test(field) || ! /[A-Z]/.test(field) || ! /[0-9]/.test(field)) return "Passwords require one each of a-z, A-Z and 0-9.\n"
              return ""
      }
  
      function validateAge(field) {
              if (isNAN(field)) return "No Age was entered.\n"
              else if (field &lt; 18 || field &gt; 110) return "Age must be between 18 and 110.\n"
              return ""
      }
  
      function validateEmail(field) {
              if (field == "") return "No Email was entered.\n"
                      else if (!((field.indexOf(".") &gt; 0) &amp;&amp; (field.indexOf("@") &gt; 0)) || /[^a-zA-Z0-9.@_-]/.test(field)) return "The Email address is invalid.\n"
              return ""
      }
      &lt;/script&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
      &lt;table class="signup" border="0" cellpadding="2" cellspacing="5" bgcolor="#eeeeee"&gt;
      &lt;th colspan="2" align="center"&gt;Signup Form&lt;/th&gt;
      &lt;form method="post" action="adduser.php" onSubmit="return validate(this)"&gt;
      &lt;tr&gt;&lt;td&gt;Forename&lt;/td&gt;&lt;td&gt;&lt;input type="text" maxlength="32" name="forename" /&gt;&lt;/td&gt;
      &lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Surname&lt;/td&gt;&lt;td&gt;&lt;input type="text" maxlength="32" name="surname" /&gt;&lt;/td&gt;
      &lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Username&lt;/td&gt;&lt;td&gt;&lt;input type="text" maxlength="16" name="username" /&gt;&lt;/td&gt;
      &lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Password&lt;/td&gt;&lt;td&gt;&lt;input type="text" maxlength="12" name="password" /&gt;&lt;/td&gt;
      &lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Age&lt;/td&gt;&lt;td&gt;&lt;input type="text" maxlength="3" name="age" /&gt;&lt;/td&gt;
      &lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Email&lt;/td&gt;&lt;td&gt;&lt;input type="text" maxlength="64" name="email" /&gt;&lt;/td&gt;
      &lt;/tr&gt;&lt;tr&gt;&lt;td colspan="2" align="center"&gt;
      &lt;input type="submit" value="Signup" /&gt;&lt;/td&gt;
      &lt;/tr&gt;&lt;/form&gt;&lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>