My problem with the following code is that it does not let me enter with the users and passwords of the first three positions of the arrays, but it does let me enter with the last one and I want it to let me enter with each user and their respective password, for example user position 1 with password position 1.
I did it with an if nested in a for and reading the code should work, but it doesn’t.
function validar() {
var nombre = document.Formulario.nombre.value;
var password = document.Formulario.password.value;
var indice = document.Formulario.rol.value;
var users = Array('Ruben', 'Juan', 'Pedro', 'Luis');
var pass = Array('1234', 'admin', 'abcd', 'password');
if (nombre == '' || password == '' || indice == 0) {
alert("Faltan campos por llenar");
} else {
alert("Campos llenos :)");
}
for (let i = users.length - 1; i >= 0; i--) {
if (nombre == users[i] && password == pass[i]) {
alert('Bienvenido ' + nombre);
break;
} else {
alert('Datos incorrectos');
break;
}
}
}
<form name="Formulario" method="get" action="recibe.php">
<input type="text" name="nombre" id="nombre" placeholder="Usuario" /><br>
<input type="text" name="password" id="password" placeholder="Password" /><br>
<select name="rol" id="rol">
<option value="0">Selecciona</option>
<option value="1">Gerente</option>
<option value="2">Ejecutivo</option>
</select><br>
<input onclick="validar(); return false;" type="submit" value="Enviar" />
</form>
2
Answers
You can use
indexOf
for your conditional check instead of using a for loop