title: Form Validation on submit using javascript
body: <p>I want to validate a form using javascript when the user submits the form, this is my code, </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>document.getElementById("form").addEventListener("submit", function(event){
      var elements = document.getElementById("form").elements;
      var error = 0;
      for(var i = 0; i &lt; elements.length; i++)
      {
              //code
              if(elements[i].type == "checkbox" || elements[i].type == "radio") {
                  var check_box = document.getElementById(elements[i].id).checked;
                    if (check_box == false) {
                        //code
                        error++;
                        var checkbox_div = elements[i].parentNode.parentNode.id;
                        produce_warning("you must agree to proceed", checkbox_div, elements[i].id);
                    }
              }
              
              else {
                  if (elements[i].value == "" || elements[i].value == null) {
                      //code
                      error++;
                      produce_warning("you can not leave this field empty", elements[i].parentNode.id, elements[i].id);
                  }
                  
              }
      }
      
      if (error &gt; 0) {
          //code
          event.preventDefault();
      }
      else{
          return true;
      }
      
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here the event.preventDefault() statement is not executing, please help me thank you.</p>