I want that when I type a , it changes to a . on an input that only accepts numbers. At the moment I have managed to get the input to only accept numbers, but I am missing the part of changing the , for a point But I am a bit lost with the subject.
Could someone tell me where to go to do it? I have seen code that does it once you send the form, but I try to do it on the fly and I have not been able to adapt the 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 doesn't work :(
function Poner_Punto() {
if(event.keyCode==190) { event.keyCode=188; }
}
You can use the event
keyup
and also usereplace
The same goes for you:
Given an input in html with id equal to "text":
We capture the keypress event and if the pressed key is the "," (with code which 44), we cancel the pending events and where a "," was going to be written, we put a ".":
Run this in the function on each press, it should automatically replace all , with . (after each press)