title: Cannot return from outside a function or method?
body: <p>While developing a web application I want to perform certain validation check and only after sucesssful validation I need to post the form and redirect control to next page.  </p>
  
  <p>JavaScript code:</p>
  
  <pre><code>function fnCheckEmptyField()
  {
      var strDomain = document.getElementsByName("txtIIDN").value;
      if (strDomain == null)
      {
          document.getElementById("lblValidityStatus").innerHTML = "";
          document.getElementById("lblValidityStatus").innerHTML = "Domain Name Field Can't be Left Blank";
          return false;
      }
      else
      {
          return true; 
      }
  }
  </code></pre>
  
  <p>Relevant HTML code:</p>
  
  <pre><code>&lt;form action="Result.jsp" name="validityCheck" onsubmit="return fnCheckEmptyField()"&gt;
      &lt;input type="text" id="txtIIDN"/&gt;
      &lt;input type="submit" id="btnValidityCheck" value="Check Validity" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The line <code>onsubmit="return fnCheckEmptyField()"</code> shows an error <em>Cannot return from outside a function or method</em> and after execution of the JavaScript function form is getting submitted regardless the text field is blank or not.</p>
  
  <p>I have placed alerts inside <code>if</code> condition and it is sure that if field is empty function returns <code>false</code>.</p>
  
  <p>I don't know what's wrong with my code and why this errors with <em>Cannot return from outside a function or method</em>.</p>
  
  <p>What's the cause and how can I solve it?</p>