Solo permitir numero / letras

  • Autor Autor Daniel Muñoz
  • Fecha de inicio Fecha de inicio
Daniel Muñoz

Daniel Muñoz

1
Pi
Programador
Verificado
Verificación en dos pasos activada
Verificado por Binance
Suscripción a IA
Hola betas.

Tengo rato mirando unas guías en Internet sobre como hacer esta validación.

Esta me funciona perfectamente, ( Validar solo números o letras en Javascript - Garabatos Linux ) pero no me permite agregar las Ñ - ñ ya intente agregando su Keycode como true, pero igual no me deja.

Y esta esta otra ( https://www.youtube.com/watch?v=p-5eNidpjBs ) esta me devuelve como cero las teclas "suprimir - flechas derecha e izquierda )

Que otra opción tengo?

Acá el código de cada una de las guías.

Insertar CODE, HTML o PHP:
#guia de Garabatoslinux
function soloLetras(e) {
  teclas = (document.all) ? e.keyCode : e.which;
  if (teclas >= 65 && teclas <= 90) { return true;}
  if (teclas === 8) { return true; }
  if (teclas === 9) { return true; }
  if (teclas === 32) { return true; }
  if (teclas === 241) { return true; }
  if (teclas >= 96 && teclas <= 105) { return false;}
  if (teclas === 106) { return false;}
  if (teclas === 107) { return false;}
  if (teclas === 111) { return false;}
  if (teclas === 109) { return false;}
  if (teclas === 110) { return false;}
  patrones = /[a-zA-Z]/;
  tee = String.fromCharCode(teclas);
  return patrones.test(tee);
}
Insertar CODE, HTML o PHP:
#guia de youtube.
function beta(e) {
  key = e.KeyCode || e.which;
  teclado = String.fromCharCode(key);
  numeros = "0123456789";
  especiales = [8,46,32];
  teclado_especial = false;
  for (var i in especiales) {
    if (key === especiales[i]) {
      teclado_especial = true;
      break;
    }
  }
  if ( numeros.indexOf(teclado)=== -1 && !teclado_especial) {
    alert (key);
    return false;
    
  }


Si pueden ayudarme les agradecería mucho.
 
HTML:
<input type="text" onkeypress="return validar(event)" />

Insertar CODE, HTML o PHP:
function validar(e)
{
	var codigo = e.which;
	var tecla = String.fromCharCode(codigo).toLowerCase();

	var valido = 'abcdefghijklmnñopqrstuvwxyz0123456789';

	return ( valido.indexOf(tecla) != -1 );
}
 
Insertar CODE, HTML o PHP:
function validar(e)
{
	var codigo = e.which;
	var tecla = String.fromCharCode(codigo).toLowerCase();

	var valido = 'abcdefghijklmnñopqrstuvwxyz0123456789 ';

	if ( codigo==0 || codigo==8 ) return true;

	return ( valido.indexOf(tecla) != -1 );
}
 
Atrás
Arriba