title: HTML Form Check Empty Fields
body: <p>I have implemented input validation in my HTML form. However, it does not prompt the user in case of any errors and still proceeds to send its data to the test server.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;style&gt;
          input {margin: 10px 5px 10px 5px;}
          select {margin: 10px 5px 10px 5px;}
      &lt;/style&gt;
      &lt;script language="JavaScript"&gt;
      function validate(form1)
      { if (form1.name.value == "" || form1.surname.value == "" || form1.email.value == "" || form1.dob.value == "")
          { alert("One of the field is empty.")
              form1.firstname.focus()
              form1.firstname.select()
              form1.lastname.focus()
              form1.lastname.select()
              form1.email.focus()
              form1.email.select()
              form1.dob.focus()
              form1.dob.select()
  
              return false
          }
      return true}
      &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form method="GET" action="http://www.test/cgi-bin/reply.pl"
      onSubmit="return validate(this)"&gt;
          First Name&lt;input type="text" name="firstname"&gt;&lt;br&gt;
          Last Name&lt;input type="text" name="lastname"&gt;&lt;br&gt;
          Email Address&lt;input type="email" name="email"&gt;&lt;br&gt;
          Date of Birth&lt;input type="date" name="dob"&gt;&lt;br&gt;
          Favourite Sport
          &lt;select name="sports"&gt;&lt;br&gt;
          &lt;option value="athletics"&gt;Athletics&lt;/option&gt;&lt;br&gt;
          &lt;option value="basketball"&gt;Basketball&lt;/option&gt;&lt;br&gt;
          &lt;option value="cricket"&gt;Cricket&lt;/option&gt;&lt;br&gt;
          &lt;option value="football"&gt;Football&lt;/option&gt;&lt;br&gt;
          &lt;option value="hockey"&gt;Hockey&lt;/option&gt;&lt;br&gt;
          &lt;option value="swimming"&gt;Swimming&lt;/option&gt;&lt;br&gt;
          &lt;option value="tennis"&gt;Tennis&lt;/option&gt;&lt;br&gt;
          &lt;/select&gt;&lt;br&gt;
          &lt;input type="Submit"&gt; &lt;input type="reset"&gt;
      &lt;/form&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>