javascript – How can I disable dots '.' and commas ',' in a text input?

Question:

Greetings, I need that in a text input (text input) like:

<input type="text" id="txtNombre" name="txtNombre" value="" placeholder="Ej: Juan" required="" />

do not let me place ' . ' and ' , '

Answer:

In vanilla JS:

function noPuntoComa( event ) {
  
    var e = event || window.event;
    var key = e.keyCode || e.which;

    if ( key === 110 || key === 190 || key === 188 ) {     
        
       e.preventDefault();     
    }
}
<input type="text" onkeydown="noPuntoComa( event )">

KeyCode Breakdown:

  • 110 – Point on the left keyboard
  • 190 – Point on the right keyboard
  • 188 – Coma
Scroll to Top