title: Javascript form validation not empty or no whitespaces
body: <p>I'm trying to validate a form I'm creating and can't seem to understand how this works. I'm trying to make sure the field isn't empty or contains no white-spaces I have validated it server side but not client side.</p>
  
  <p>Could someone please show me the code like below to validate against being empty or having no white-spaces?</p>
  
  <p>I see these below and this is what I thought they did:</p>
  
  <pre><code>x===null // means if field is empty
  x===""  // on trying this means if the field is empty
  x===" " // and this checks if there is 1 white space
  </code></pre>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
      &lt;html&gt;
      &lt;head&gt;
      &lt;script&gt;
      function validateForm() {
          var x = document.forms["myForm"]["fname"].value;
          if (x===null || x===""|| 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;
      &lt;/form&gt;
      &lt;/body&gt;
      
      &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>