title: The HTML form submits even if the input element is empty
body: <p>I have a HTML form and I use JavaScript to check for empty fields to stop form submission but the form submits to the URL in action attribute even if any one or all fields are empty.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Sign Up&lt;/title&gt;
      &lt;script type="text/javascript"&gt;
          function validate() {
  
  
          var fname = document.forms["form1"]["fname"].value;
          var lname = document.forms["form1"]["lname"].value;
          var ID = document.forms["form1"]["ID"].value;
          var Password = document.forms["form1"]["Password"].value;
  
          if(fname == null || lname == null || ID == null || Password == null)
          {
              alert("Please enter all the fields");
              return false;
          };
  
      }
      &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body style = "font-family: Lato"&gt;
  
      &lt;div style="padding-left: 10%"&gt;
      &lt;h1&gt;Welcome to my site&lt;/h1&gt;&lt;br&gt;
      &lt;/div&gt;
      &lt;div style="text-align: center"&gt;
         &lt;form  name="form1" onsubmit="return validate()" method = "post" id="form1"  action = "http://127.0.0.1:2906/success" style="padding-top: 2%"&gt;
             &lt;label id="user" style="font-size: 20px"&gt;Username&lt;/label&gt;&lt;br&gt;
             &lt;input type="text" name = "ID" placeholder = "Username" style = "padding-left: 0.2%;"&gt;&lt;br&gt;&lt;br&gt;
             &lt;label style="font-size: 20px"&gt;First name&lt;/label&gt;&lt;br&gt;
             &lt;input type="text" name = "fname" id="fname" placeholder = "First name" style = "padding-left: 0.2%"&gt;&lt;br&gt;&lt;br&gt;
             &lt;label style="font-size: 20px"&gt;Last name&lt;/label&gt;&lt;br&gt;
             &lt;input type="text" name = "lname" placeholder = "Last name" style = "padding-left: 0.2%"&gt;&lt;br&gt;&lt;br&gt;
             &lt;label style="font-size: 20px"&gt;Password&lt;/label&gt;&lt;br&gt;
             &lt;input type = "password" name = "Password" placeholder = "Password" style = "padding-left: 0.2%"&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
             &lt;input type = "submit"  value = "Sign up" style="width:10%;height: 40px"&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>