How can I disable the points '.' and commas ',' in a text entry?

4

Greetings, I need a text entry (text input) like:

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

Do not let me put '. ' and ', '

    
asked by Kevin Delva 22.12.2017 в 04:49
source

2 answers

6

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 )">

Breakdown KeyCode :

  • 110 - Point on the left keyboard
  • 190 - Point on the right keyboard
  • 188 - Coma
answered by 22.12.2017 / 07:10
source
1

Good friend you can use the library Mask and weigh it as a parameter a regular expression.

Regular expression

/[^.,]/

This expression tells us that you can enter any character except the . and the ,

Functional example

$("#txtNombre").mask('ZZ',{translation:  {'Z': {pattern: /[^.,]/, recursive: true}}});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.13/jquery.mask.min.js"></script>
<input type="text" id="txtNombre" name="txtNombre" value="" placeholder="Ej: Juan" required="" />

I hope it is what you are looking for greetings.

    
answered by 22.12.2017 в 05:42