I have the following code JS
:
var botao = document.querySelector('input[type="submit"]');
botao.addEventListener('click', function(e) {
let form = document.querySelector('form');
let ipt = {
nome: form.nome.value,
senha: form.senha.value
}
var users = [
{nome: "Jorge", senha: "123"},
{nome: "Joao", senha: "joao"},
{nome: "Maria", senha: "maria"},
{nome: "José", senha: "jose"},
{nome: "Ana", senha: "ana"},
]
for(let i = 0; i<users.length; i++) {
if (ipt.nome == users[i].nome && ipt.senha == users[i].senha) {
e.preventDefault();
alert('Bem vindo ' + form.nome.value + " !");
window.location.href= "http://www.google.com.br";
}
else {
e.preventDefault();
alert('Dados incorretos!');
}
}
});
and the following html
:
<form name="form1" id="form1">
<input name="nome" type="text" placeholder="Usuário "/>
<input name="senha" type="password" placeholder="Senha "/>
<input type="submit" value="Login"/>
</form>
I'm doing a check according to the indexes of the users
vector, the problem is that: When checking that the user exists, it will print Dados incorretos
until you get the correct index and print the welcome message and make the redirect.
See the example below:
var botao = document.querySelector('input[type="submit"]');
botao.addEventListener('click', function(e) {
let form = document.querySelector('form');
let ipt = {
nome: form.nome.value,
senha: form.senha.value
}
var users = [
{nome: "Jorge", senha: "123"},
{nome: "Joao", senha: "joao"},
{nome: "Maria", senha: "maria"},
{nome: "José", senha: "jose"},
{nome: "Ana", senha: "ana"},
]
for(let i = 0; i<users.length; i++) {
if (ipt.nome == users[i].nome && ipt.senha == users[i].senha) {
e.preventDefault();
console.log('Bem vindo ' + form.nome.value + " !");
//window.location.href= "http://www.google.com.br";
}
else {
e.preventDefault();
console.log('Dados incorretos!');
}
}
});
<form name="form1" id="form1">
<input name="nome" type="text" value="Ana" placeholder="Usuário "/>
<input name="senha" value="ana" type="password" placeholder="Senha "/>
<input type="submit" value="Login"/>
</form>
See that it prints 4 messages from dados incorretos
to only after printing bem vindo
.
Can anyone help me?