javascript – How to block special characters in field


How can I not allow the user to enter special characters such as *-/+.,:;[]{}ªº^~?<> in the question field?


I made it this way, it will only allow letters and numbers:

       document.getElementById("teste").onkeypress = function(e) {
         var chr = String.fromCharCode(e.which);
         if ("1234567890qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM".indexOf(chr) < 0)
           return false;
<input type="text" name="name" id="teste" />
Scroll to Top