title: 'Error: A 'return' statement can only be used within a function body' - HTML Form
body: <p>I am getting an error in my old HTML form onsubmit return statement. It was working perfectly fine before but recently the same statement is showing an error. I am attaching the screenshot of the error <a href="https://i.stack.imgur.com/DJ0JH.jpg" rel="noreferrer">HTML form return statement error</a></p>
  <p><code>&lt;form action=&quot;index.html&quot; onsubmit = 'return validate();'&gt;</code></p>
  <ul>
  <li>Error: A 'return' statement can only be used within a function body.*</li>
  </ul>
  <p>I have tried removing 'return' but the form gets submitted even if the validate function returns false.</p>
  <p>Then I added preventDefault(), but now the form doesn't get submitted or redirected even if the validate function returns true.
  <code>&lt;form action=&quot;index.html&quot; onsubmit = ' preventDefault() validate();'&gt;</code></p>
  <p>JS code</p>
  <pre><code>function validate() {
      
      if (em.value.trim() == &quot;&quot; || pas.value.trim() == &quot;&quot;) {
          alert(&quot;Fields not filled&quot;);
          em.style.border = &quot;2px solid red&quot;;
          pas.style.border = &quot;2px solid red&quot;;
  
          return false;
      }
  
      else if (regexp.test(em.value)) {
          er.innerHTML = &quot;VALID!&quot;;
          er.style.color = &quot;green&quot;;
          return true;
      }
      else {
          er.innerHTML = &quot;INVALID E-MAIL!!&quot;; //!Alert this is important line
          er.style.color = &quot;red&quot;;
          em.style.border = &quot;2px solid red&quot;;
          return false;
      }
  
  }
  </code></pre>