title: Validate form with JavaScript using onsubmit
body: <p>I have a website with an existing form, that submits an email address to a third party website (example.com). Now I'd like to validate the email input-field before submitting the form by using JavaScript.</p>
  <p><strong>My JavaScript</strong></p>
  <pre><code>function isValidEmailAddress(emailAddress) {
      var pattern = new RegExp(/^((&quot;[\w-\s]+&quot;)|([\w-]+(?:\.[\w-]+)*)|(&quot;[\w-\s]+&quot;)([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
      return pattern.test(emailAddress);
  }
  
  function formCheck() {
      // Fetch email-value
    sEmail = document.myForm.recipientEmail.value ;
      // value empty?
      if (sEmail == 0)
          {
              alert('Enter Email-Address.');
              return false;
          }
      else
          {
              // value valid?
              if (!isValidEmailAddress(sEmail))
                  {
                      alert('Enter valid Email-Address.');
                      return false;
                  }
              else
                  {
                      return true;
                  }
          }
  }   
  </code></pre>
  <p><strong>Existing form tag</strong></p>
  <pre><code>&lt;form name=&quot;myForm&quot; action=&quot;http://example.com&quot; method=&quot;post&quot; onsubmit=&quot;return formCheck();&quot;&gt;
  </code></pre>
  <p><strong>Existing email input field</strong></p>
  <pre><code>&lt;input type=&quot;text&quot; class=&quot;form-text&quot; name=&quot;email&quot; id=&quot;recipientEmail&quot;&gt;
  </code></pre>
  <p><strong>Existing <em>submit button</em></strong></p>
  <pre><code>&lt;input type=&quot;image&quot; src=&quot;some_pic.gif&quot; alt=&quot;OK&quot; id=&quot;submit_form&quot; onclick=&quot;document.onlineForm.submit();&quot; value=&quot;send&quot; name=&quot;submit_button&quot;&gt; 
  </code></pre>
  <p>Now, as soon as I hit the submit button, the form is submitted and my JavaScript seems to be ignored. What am I doing wrong?</p>