Daniel Muñoz
1
Pi
Programador
Verificado
Verificación en dos pasos activada
Verificado por Whatsapp
¡Ha verificado su Paypal!
Verificado por Binance
¡Usuario popular!
Suscripción a IA
<script type="text/javascript">
function validarLetras(e) { // 1
tecla = (document.all) ? e.keyCode : e.which;
if (tecla==8) return true; // backspace
if (tecla==32) return true; // espacio
if (e.ctrlKey && tecla==86) { return true;} //Ctrl v
if (e.ctrlKey && tecla==67) { return true;} //Ctrl c
if (e.ctrlKey && tecla==88) { return true;} //Ctrl x
patron = /[a-zA-Z]/; //patron
te = String.fromCharCode(tecla);
return patron.test(te); // prueba de patron
}
</script>
<input type="text" name="letras" id="letras" onkeydown="return validarLetras(event)"/>
function validar_numeros(numero)
{
if (!/^([0-9])*$/.test(numero))
alert("El campo solo debe contener numeros");
}
.test es para testear... lo que hace validar que solo sean numeros... (permite expresiones regulares)
JavaScript test() Method
<script type="text/javascript">
function validarLetras(e) { // 1
tecla = (document.all) ? e.keyCode : e.which;
if (tecla==8) return true; // backspace
if (tecla==32) return true; // espacio
if (e.ctrlKey && tecla==86) { return true;} //Ctrl v
if (e.ctrlKey && tecla==67) { return true;} //Ctrl c
if (e.ctrlKey && tecla==88) { return true;} //Ctrl x
patron = /[a-zA-Z]/; //patron
te = String.fromCharCode(tecla);
return patron.test(te); // prueba de patron
}
</script>
Me gustaría que me explicaras el primer ejemplo, donde solo se permiten ciertas teclas.
Insertar CODE, HTML o PHP:<script type="text/javascript"> function validarLetras(e) { // 1 tecla = (document.all) ? e.keyCode : e.which; if (tecla==8) return true; // backspace if (tecla==32) return true; // espacio if (e.ctrlKey && tecla==86) { return true;} //Ctrl v if (e.ctrlKey && tecla==67) { return true;} //Ctrl c if (e.ctrlKey && tecla==88) { return true;} //Ctrl x patron = /[a-zA-Z]/; //patron te = String.fromCharCode(tecla); return patron.test(te); // prueba de patron } </script>
Si justamente estaba investigando de eso.lo que hace es verificar el numero de cada tecla (cada tecla tiene un numero asignado en el codigo ascii...) ejemplo el 32 es el espacio, el 13 es el enter, el 8 es la barra de brrar... etc...
(document.all) ? e.keyCode : e.which;
patron = /[a-zA-Z]/; //patron
te = String.fromCharCode(tecla);
return patron.test(te); // prueba de patron
Si justamente estaba investigando de eso.
Javascript Char Codes (Key Codes) - Cambia Research
Pero aun tengo interrogantes.
¿Para que sirve?Insertar CODE, HTML o PHP:(document.all) ? e.keyCode : e.which;
Insertar CODE, HTML o PHP:patron = /[a-zA-Z]/; //patron te = String.fromCharCode(tecla); return patron.test(te); // prueba de patron
Para que sirven?
el 1 es como si fuera un if... lo que hace es ver si el keycode existe lo coloca al document.all sino usa e.wich (es por compatibilidad de navegadores)
el 2 es lo que te explike antes... usa el test para verificar si son az AZ (solo letrax)
function numeros (e) {
numero = ( document.all ) ? e.KeyCode : e.which;
if ( numero >= 48 && numero <= 57 ) { return true; }
if ( numero >= 96 && numero <= 105 ) { return true; }
if ( numero === 8 ) { return true };
patron = /[0-9]/;
v = String.fromCharCode(numero);
return patron.test(v);
}
Okay gracias brother.
Mira lo que hice.
Insertar CODE, HTML o PHP:function numeros (e) { numero = ( document.all ) ? e.KeyCode : e.which; if ( numero >= 48 && numero <= 57 ) { return true; } if ( numero >= 96 && numero <= 105 ) { return true; } if ( numero === 8 ) { return true }; patron = /[0-9]/; v = String.fromCharCode(numero); return patron.test(v); }
Donde solo permito los numero del teclado y en numpad. Pero quiero bloquear los signos ( !"#$%&/()= ) como hago?
usas el ascii de esos... y regresas FALSE
if ( numero >= 33 && numero <= 35 ) { return false; }
if ( numero === 33 ) { return false; }
Ya lo intente, según su numero ASCII ( ASCII - Wikipedia, la enciclopedia libre ) colocándolo en false, pero igual permite colocar los signos.
Insertar CODE, HTML o PHP:if ( numero >= 33 && numero <= 35 ) { return false; }
Insertar CODE, HTML o PHP:if ( numero === 33 ) { return false; }
¿Alguna otra idea?
<input type="text" name="letras" id="letras" onkeydown="return numeros(event)"/>
<textarea id="pepito"></textarea>
<script>
function numeros (e) {
numero = ( document.all ) ? e.KeyCode : e.which;
document.getElementById('pepito').value = document.getElementById('pepito').value + numero + "\n";
if (e.shiftKey){
if(numero >= 33 && numero <= 35) { return false; }
if(numero >= 48 && numero <= 57) { return false; }
}
if ( numero >= 48 && numero <= 57 ) { return true; }
if ( numero >= 96 && numero <= 105 ) { return true; }
if ( numero === 8 ) { return true };
patron = /[0-9]/;
v = String.fromCharCode(numero);
return patron.test(v);
}
</script>
En mi computador es igual, para sacar los signos !"#$%&/()= debo presionar la tecla "shift" y presionar la tecla correspondiente por ejemplo. Para el " ! " debe ser "Shift + 1"el texarea es para ver el numero de la tecla... en el teclado de mi notebook los !"#$%&/()= son con la tecla shift y tienen el mismo valor que los numeros... por eso verifico si preciono la tecla shift y si coincide con alguno de esos numero...HTML:<input type="text" name="letras" id="letras" onkeydown="return numeros(event)"/> <textarea id="pepito"></textarea> <script> function numeros (e) { numero = ( document.all ) ? e.KeyCode : e.which; document.getElementById('pepito').value = document.getElementById('pepito').value + numero + "\n"; if (e.shiftKey){ if(numero >= 33 && numero <= 35) { return false; } if(numero >= 48 && numero <= 57) { return false; } } if ( numero >= 48 && numero <= 57 ) { return true; } if ( numero >= 96 && numero <= 105 ) { return true; } if ( numero === 8 ) { return true }; patron = /[0-9]/; v = String.fromCharCode(numero); return patron.test(v); } </script>
En mi computador es igual, para sacar los signos !"#$%&/()= debo presionar la tecla "shift" y presionar la tecla correspondiente por ejemplo. Para el " ! " debe ser "Shift + 1"
En pocas palabras probe el codigo que colocaste, pero no lo entendi :s
document.getElementById('pepito').value = document.getElementById('pepito').value + numero + "\n";
if (e.shiftKey){
if(numero >= 33 && numero <= 35) { return false; }
if(numero >= 48 && numero <= 57) { return false; }
}
no es nada raro... el textarea ese sacalo al igual queHTML:document.getElementById('pepito').value = document.getElementById('pepito').value + numero + "\n";
Lo que te interesa a vos es esto:
HTML:if (e.shiftKey){ if(numero >= 33 && numero <= 35) { return false; } if(numero >= 48 && numero <= 57) { return false; } }
se verifica si la tecla shift se preciona, si es asi, verificas si se esta precionando conjuntamente con el shift algun numero para los simbolos !"#$%&/
patron = /[0-9]/;
v = String.fromCharCode(numero);
return patron.test(v);
Probé agregando lo que me sugeriste pero no me funciono. La solución la encontre leyendo en internet (tenia rato investigando y viendo otros ejemplos.
Cambie el onkeydown por onkeypress y ya no permite los signos, me imagino porque el onkeypress solo permite 1 letra al mismo tiempo.
Lo que no me queda muy claro es la funcionalidad de esto, aunque ya me explicaste.
Utilizamos cookies y tecnologías similares para los siguientes fines:
¿Aceptas las cookies y estas tecnologías?
Utilizamos cookies y tecnologías similares para los siguientes fines:
¿Aceptas las cookies y estas tecnologías?