Of a form HTML
without being able to touch it, we depend on programming a .js in Javascript.
We have several fields input
in which you must control the introduction of correct information (only letters in fields such as name or surname, and only numbers in CP).
How could this code retouched in HTML be transformed into a code without touching the HTML? Let me explain.
First form. Editable HTML + JS:
<input type="text" name="nombre" id="idnombre" size="20" onKeyPress="return soloLetras(event);" onKeyUp="javascript:this.value=this.value.toUpperCase();">
<input type="text" name="apellidos" id="idapellidos" size="40" onKeyPress="return soloLetras(event);">
function soloLetras(e){
var key = e.keyCode || e.which;
var tecla = String.fromCharCode(key).toLowerCase();
var letras = " áéíóúabcdefghijklmnñopqrstuvwxyz";
var especiales = "8-37-39-46";
var tecla_especial = false
for(var i in especiales){
if(key == especiales[i]){
tecla_especial = true;
break;
}
}
if(letras.indexOf(tecla) == -1 && !tecla_especial){
return false;
}
}
Second form. HTML NOT editable + JS:
<input type="text" name="nombre" id="idnombre" size="20">
<input type="text" name="apellidos" id="idapellidos" size="40">
Where do I have the error?
window.onload = function(){
miformulario.nombre.addEventListener("keypress", function(){
return soloLetras(event);
}, false);
miformulario.nombre.addEventListener("keyup", function(){
this.value = this.value.toUpperCase();
}, false);
miformulario.apellidos.addEventListener("keypress", function(){
return soloLetras(event);
}, false);
}