title: Validating form before submitting
body: <p>I could submit a form and validate it using a simple anonymous function in javascript as,</p>
  
  <pre class="lang-javascript prettyprint-override"><code> document.getElementById('myForm').onsubmit = function() {
       if(document.getElementById('email').value == "") {
           document.getElementById('errormessage').innerHTML = "Please provide at least an email address";
           return false;
       } else {
           return true; // Note this
       }
   };
  </code></pre>
  
  <p>In the example above using anonymous function, I could simply assign the return value when the document is submitted and prevent the page from being submitted while in the example below I use <code>addEventListener</code> to add event when the form is submitted and it is further evaluated by validate function.  I have also placed the return value to be true or false but this approach does not seem to work and the page gets submitted. I think I am wrong in returning the value</p>
  
  <pre class="lang-javascript prettyprint-override"><code>function addEventHandler(node, evt, func) {
      if(node.addEventListener)
          node.addEventListener(evt, func);
      else
          node.attachEvent("on" + evt, func);
  }
  function initializeAll() {
      addEventHandler(document.getElementById('myform'), 'submit', validate);
  }
  function validate() {
      if(document.getElementById('email').value == ""){
          document.getElementById("errormessage").innerHTML = "Please provide at least an email ";
          return false;
      } else {
          return true;
      }
  }
  addEventHandler(window, 'load', initializeAll);
  </code></pre>
  
  <p>How should I return value this way so as to prevent the form from submitting or submitting the form?</p>