Avoid Special Characters on Key press using JavaScript

HTML

  1. <input type="text" onkeypress="avoidSplChars(event)" >  
JavaScript function
  1. function avoidSplChars(e) {  
  2.     e = e || window.event;  
  3.     var bad = /[^\sa-z\d]/i,  
  4.         key = String.fromCharCode(e.keyCode || e.which);  
  5.     if (e.which !== 0 && e.charCode !== 0 && bad.test(key)) {  
  6.         e.returnValue = false;  
  7.         if (e.preventDefault) {  
  8.             e.preventDefault();  
  9.         }  
  10.     }  
  11. }  
Hope this helps!!