<input> Reject certain character

Asked

Viewed 27 times

0

For example how it would look mine tag/script to reject the comma of a <input> in real time? I don’t want to allow this character to be placed on input and after the invalidation I want that as soon as the person keystrokes the character he does not "prints" in the input.

1 answer

0


Just pick up the key code with onkeydown and return false if it is equal to 188 (alphanumeric keypad comma) or 110 (numeric keypad comma):

document.getElementById("campo").onkeydown = function(e){
   var tecla = e.keyCode || e.which;
   if(tecla == 188 || tecla == 110) return false;
}
<input id="campo" autofocus>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.