title: How to validate html form for null/empty fields with JavaScript using ID assigned to form
body: <p>I'm trying to validate my html form with JavaScript as so it iterates trough out all input fields and checks for empty/null fields. </p>
  
  <p>I found a way to validate for null on w3s (code below) but I want to modify the function as so it checks for all fields on the form using a specific <code>id</code> that I have assigned to the entire form, instead of only targeting one field. </p>
  
  <pre><code>function validateForm() {
     var x = document.forms["myForm"]["fname"].value;
     if ( x == null || x == "" ) {
        alert("First name must be filled out");
        return false;
     }
  }
  &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post"&gt;
      First name: &lt;input type="text" name="fname"&gt;
      &lt;input type="submit" value="Submit"&gt;
  </code></pre>