title: How to call "onsubmit" when "javascript:document.forms[0].submit()" is used?
body: <p>I am new to javascript and I am facing the following issue:
  I made a simple date accepting form which checks the format before submission. In case the entered date is not in the desired format, it shows an error. Now, I replace the submit button on the form with a link using another javascript function. I link it to <code>javascript:document.forms[0].submit()</code>. This time, the format checking function is not called during form submission. It gets submitted without the check. It would be nice if someone could tell me how to overcome this issue.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function checkDate() {
    var date = document.getElementById("date");
    var dt = date.value;
    var flag = 1;
    var msg = "";
    var errorHere = document.getElementById("spam");
    for (var i = 0; i &lt; dt.length; i++) {
      if (i == 2 || i == 5) {
        if (dt[i] != '-') {
          flag = 0;
          break;
        }
        continue;
      }
      if (dt[i] &lt; '0' || dt[i] &gt; '9') {
        flag = 0;
        break;
      }
    }
    if (dt.length != 10) flag = 0;
    if (dt.length == 0) flag = -1;
    if (flag == 0)
      msg += "Invalid format.";
    else if (flag == -1)
      msg += "Empty field."
    else msg += "OK.";
    errorHere.firstChild.nodeValue = msg;
    if (flag == 1) return true;
    else return false;
  }
  
  function submitter() {
    var abc = confirm("Would you like to switch to link?");
    if (abc == true) {
      var i = 0,
        now;
      while (++i) {
        now = document.getElementsByTagName("input")[i];
        if (!now) return;
        if (now.type == "submit") break;
      }
      var latest = document.createElement("a");
      latest.href = "javascript:document.forms[0].submit()";
      var text = document.createTextNode(now.value);
      latest.appendChild(text);
      now.parentNode.replaceChild(latest, now);
    }
  }
  window.onload = submitter;</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;head&gt;
    &lt;script type="text/javascript" src="date.js"&gt;
    &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;h1&gt;Date checker&lt;/h1&gt;
    &lt;form action="eventssearch.php" method="post" onsubmit="return checkDate();"&gt;
      &lt;p&gt;
        &lt;label for="date"&gt;Date in the format DD-MM-YYYY:&lt;/label&gt;
        &lt;br /&gt;
        &lt;input type="text" id="date" name="date" /&gt;
        &lt;input type="submit" value="Check" /&gt;
        &lt;br /&gt;(example 01-01-1970)
        &lt;p id="spam"&gt;&lt;/p&gt;
      &lt;/p&gt;
    &lt;/form&gt;
  &lt;/body&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Gramercy...</p>