javascript – How to block special characters in field

Question:

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

Answer:

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