title: Why the form is getting submitted even if all the fields are empty?
body: <p>Why my form is getting submitted even if I leave all fields empty. I can't figure out what the problem is. The <code>if</code> loops looks fine to me.</p>
  
  <p>This is my code</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en" dir="ltr"&gt;
  
  &lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;title&gt;Javascript form check&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"&gt;
  
  &lt;body style="margin-top: 30px; margin-left: 30px;"&gt;
  
    &lt;form method="POST" action="form-handler" onsubmit="return checkForm(this);"&gt;
      &lt;p&gt;First Name &lt;input type="text" size="32" name="first_name"&gt;&lt;/p&gt;
      &lt;p&gt;Email Address &lt;input type="text" size="32" name="email"&gt;&lt;/p&gt;
      &lt;p&gt;Phone Number &lt;input type="number" size="32" name="phoneno"&gt;&lt;/p&gt;
      &lt;p&gt;Password &lt;input type="password" size="32" name="pass"&gt;&lt;/p&gt;
      &lt;p&gt;Verify Password &lt;input type="password" size="32" name="pass_verify"&gt;&lt;/p&gt;
      &lt;p&gt;Date of Birth &lt;input type="date" size="32" name="date"&gt;&lt;/p&gt;
      &lt;p&gt;Weight &lt;input type="text" size="32" name="weight"&gt;&lt;/p&gt;
        &lt;input type="submit"&gt;
    &lt;/form&gt;
  
  
  
    &lt;script&gt;
  
      function checkForm(form) {
        // validation fails if the input is blank
        if (form.first_name.value == "") {
          alert("Error: Input is empty!");
          form.first_name.focus();
          return false;
        }
  
  
        if (form.weight.length == 0)
        {
           alert("Invalid Weight");
           return false;
        }
  
  
  
        // regular expression to match only alphanumeric characters and spaces
        var re = /^[\w ]+$/;
  
  
  
        // validation fails if the input doesn't match our regular expression
        if (!re.test(form.first_name.value)) {
          alert("Error: Input contains invalid characters!");
          form.first_name.focus();
          return false;
        }
  
  
  
       //Code to Validate Phone Number
       var phoneno = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
       if(!(form.phoneno.match(phoneno))
          {
          alert("Number must be 10 characters");
          return false;
          }
  
  
          //Code to validate password
          var passw= /^[A-Za-z]\w{4,14}$/;
          if(!(form.pass.match(passw)))
          {
          alert('Wrong password')
          return false;
          }
  
  
          //Code to validate email
          if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value))){
  
          alert("You have entered an invalid email address!")
          return false;
         }
  
      }
    &lt;/script&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>