title: Validation for names in a text input
body: <p>I have a text input for a name. I want to allow only letters and non-consecutive white-spaces anywhere, except that white-space isn't allowed at the start or end of the input. So I have to invalidate numbers, symbols, and consecutive white-space.</p>
  
  <p>Examples:</p>
  
  <ol>
  <li><code>rohit_kumar_mehta</code></li>
  <li><code>rohit__kumar_mehta</code></li>
  <li><code>rohit_kumar__mehta</code></li>
  <li><code>_rohit_kumar_mehta</code></li>
  <li><code>__rohit_kumar_mehta</code></li>
  <li><code>rohit_kumar_mehta_</code></li>
  </ol>
  
  <p><code>_</code> -- means single white-space</p>
  
  <p><code>__</code> -- means double white-space</p>
  
  <p>String 1 is right.</p>
  
  <p>Strings 2, 3, 4, 5, and 6 are wrong.     </p>
  
  <p>I tried the following code:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;script&gt;
  
  function validateForm() {
      var x = document.myForm.fname.value;
      var spacepos = x.indexOf(" ");
      var numbers = /^[0-9]+$/;
      var n = x.split("  ");
      //x.innerHTML = n[0];
  
      var iChars = "!@#$%^&amp;*()+=-[]\\\';,./{}|\":&lt;&gt;?0123456789";
      for (var i = 0; i &lt; x.length; i++) {
          if ((iChars.indexOf(x.charAt(i)) != -1)) {
              alert("invalid...1");
              return false;
          }
          /**else if ((numbers.indexOf(x.numberAt(i)) != -1) &amp;&amp; spacepos &gt; 0) {
              alert("invalid...3");
              return false;
          }**/
      }
  
      var alphabets = /^[a-zA-Z]+$/;
  
      if ((n[0].match(alphabets) &amp;&amp; spacepos &gt; 0)) {
          alert("doublke space......1");
      }
      /*else if ((n[1].match(alphabets) &amp;&amp; spacepos &gt; 0)) {
          alert("doublke space.....2");
      }*/
      else if ((x.match(alphabets) || spacepos &gt; 0)) {   
          alert("ok..2");
          return true;
      }
      else {
          alert("invalid..2");
          return false;
      }
  
      /**if(x==" ") {
          alert("invalid..3");
          return false;
      }
      else {
          alert("ok...3")
          return true;
      }**/
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form name="myForm" 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>