Replace, by. to the flight with javascript

1

I want that when typing one, I change it for a. in an input that only accepts numbers. For the moment I have managed to get the input to only accept numbers, but I need the part to change the, for a point But I'm a little Lost with the subject.

Could someone tell me where to shoot to do it? I have seen code that you do once you send the form, but I try to do it on the fly and I have not been able to adapt code to do it as I need.

this is what I have at the moment:

function Comprobarnum(e) {
    var numeros = "0123456789,-";  // Variables que definen los caracteres permitidos
    var teclas_especiales = [37, 8, 39, 46, 190, 188, 9, 173]; //46 = Supr, 37 = flecha izquierda, 39 = flecha derecha
    var evento = e || window.event;  // Obtener la tecla pulsada
    var tecla = evento.charCode || evento.keyCode;
    var caracter = String.fromCharCode(tecla);

    var tecla_especial = false; // Comprobar si la tecla pulsada es alguna de las teclas especiales
    for(var i in teclas_especiales) {
        if(tecla == teclas_especiales[i]) { tecla_especial = true; break; }
    }
    var chachi = numeros.indexOf(caracter) != -1 || tecla_especial; // Comprobar si la tecla pulsada se encuentra en los caracteres permitidos
    return chachi;
}

I also have this, but it does not work: (

function Poner_Punto() {
if(event.keyCode==190) { event.keyCode=188; }
}
    
asked by Killpe 21.02.2018 в 16:45
source

3 answers

2

You can use the event keyup and also use replace

$('#numero').keyup(function() {
  $(this).val($(this).val().replace(',', '.'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="numero">
    
answered by 21.02.2018 / 16:54
source
2

The same goes for you:

Given an input in html with id equal to "text":

           <input id="texto" type="text" value="">

We capture the keypress event and if the pressed key is the "," (with code which 44), we cancel the pending events and where we would write a ",", we put a ".":

           <script>
                $("#texto").keypress(function(event) {
                     if(event.which == 44) { // es la coma
                          event.preventDefault();
                          event.target.value += '.';
                     }
                });
           </script>
    
answered by 21.02.2018 в 17:01
1

Execute this in the function of each pulsation, all of them should automatically be replaced, by. (after each press)

$('#yourInput').val($('#yourInput').val().split(',').join('.'));
    
answered by 21.02.2018 в 16:49