title: Validating a textbox while typing special characters and having specific length
body: <p>I have a textbox.I have to validate for special characters and max 15 characters in length.
  I have used below code for validating.</p>
  
  <pre><code>&lt;input type="text" class="textFlag" onkeyup="ValidateFlagText(this)"/&gt;
  
  
  //function checks special character entry and total 15
  //character checks
  function ValidateFlagText(textBoxObj) {
  
       //skip events for space and control keys
      if (event.keyCode != 37 &amp;&amp; event.keyCode != 39) {
          var originalValue = textBoxObj.value;
          var charCount = textBoxObj.value.length;
  
          if (!(!originalValue.match(/[_\W]/))) {
              alert("special characters not allowed");
              textBoxObj.value = originalValue.substring(0, charCount - 1);
          }
  
          if (charCount &gt; 15) {
              alert("maximum 15 characters allowed");
              textBoxObj.value = originalValue.substring(0, charCount - 1);
          }
  
          textBoxObj.value = textBoxObj.value.substring(0, 15);
      }
  }
  </code></pre>
  
  <p>One issue here is if i am holding and pressing. I have created a fiddle.
  <a href="http://jsfiddle.net/mnsscorp/QGRHP/6/" rel="nofollow">http://jsfiddle.net/mnsscorp/QGRHP/6/</a></p>
  
  <p>Thanks in advance.</p>