Good, I'm trying to make the user have to enter if or if at least a number or a letter, otherwise than error.
//Caracteres permitidos
var letras = "abcdefghijklmnopqrstuvwxyz";
var numeros = "0123456789";
var correcto = 0;
var correctonum = 0;
//Formulario
var password = document.createElement("form");
password.action = "";
password.method = "POST";
password.id = "formulario";
password.onsubmit = "return envio()";
//Ingreso de contraseña
var entrada = document.createElement("input");
entrada.type = "text";
entrada.name = "password";
entrada.id = "pass";
entrada.placeholder = "Ingrese su contraseña";
//boton
var boton = document.createElement("input");
boton.type = "submit";
boton.value = "Enviar";
boton.onclick = contra;
//div que contiene todo
var contenido = document.createElement("div");
contenido.id = "contenido";
//Añadir todo al HTML
document.body.appendChild(contenido);
contenido.appendChild(password);
contenido.appendChild(entrada);
contenido.appendChild(boton);
//Funcion para saber si la contraseña es correcta
function contra() {
entrada.value.toLowerCase;
for (var i = 0; i < entrada.value.length; i++) {
correcto = 0;
correctonum = 0;
for (var j = 0; j < letras.length; j++) {
if (letras.charAt(j) == entrada.value.charAt(i)) {
correcto++;
}
if (correcto == 0) {
alert("error,debe introducir al menos una letra");
return false;
}
}
for (var l = 0; l < numeros.length; l++) {
if (numeros.charAt(l) == entrada.value.charAt(i)) {
correctonum++;
}
if (correctonum == 0) {
alert("error,debe introducir al menos un numero");
return false;
}
}
return true;
}
}
function envio() {
if (requerido(entrada)) {
return true;
} else {
alert("vuelva a intentarlo");
}
}
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Ejercicio 1</title>
</head>
<body>
<script src="ejercicio1_ext.js"></script>
</body>
</html>
The error that I have is that the entered letter can not be a number if it is a letter or the other way around, that is why it will always throw an error.
What is the correct way to do it?