title: How To Check For Empty Fields In HTML Form With JavaScript
body: <p>I'm checking a website registration form with JavaScript code and onchange listeners.
  Empty fields/spaces need to be checked for first before checking for illegal characters, too long strings, etc.</p>
  
  <p>I've read <a href="https://stackoverflow.com/questions/154059/how-do-you-check-for-an-empty-string-in-javascript">this</a>.</p>
  
  <p>But for a null string, </p>
  
  <pre><code>   if (field.value ==="")
        alert("Empty field!");
  </code></pre>
  
  <p>this will not generate the desired alert.</p>
  
  <p>People at the end of the above thread suggested that recent browser versions might not accept such a statement.</p>
  
  <p>So, how do I sort out empty/blank/ignored fields ?</p>
  
  <p>EDIT</p>
  
  <p>I've already tried </p>
  
  <pre><code>if (!field.value)  
  </code></pre>
  
  <p>but it only provides an alert if the user has already typed some characters in the field and immediately deleted them before entering a blank field. It will not provide an alert just by clicking the mouse on it and then tabbing on to the next field. It looks like I may need to assign a null value to these form fields at the outset . .  I am using implicit adding of the changeEvent listener, i.e. on seeing a value explicitly assigned to the onchange attribute of an  element, it is activated without any addEventListener(..) statement.</p>
  
  <p>Also, </p>
  
  <pre><code>if (field.value.length == 0) 
  </code></pre>
  
  <p>does not seem to produce any alert.</p>