Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
I have stored username and password to a key login in local storage and I need to validate from home page using JavaScript code Is my code is correct?

I have stored username and password to a key login in local storage and I need to validate from home page using JavaScript code. function validlogin(event)

   function validlogin(event) {
  var user = document.getElementById('user').value;
  var psw = document.getElementById('psw').value;

  var entriesJSON = localStorage.getItem('login');
  if (!entriesJSON) {
    alert("Nothing stored!");
    event.preventDefault();
    return;
  }
  var allEntries = JSON.parse(entriesJSON);
  for (var i = 0; i < login.length; i++) {
    var entry = login[i];
    var username = entry.user;
    var password = entry.pass;
    var email = entry.email;
    if (user == storedUserName && psw == storedPassWord) {
      alert("Successfully logged in!");
      return;
    }
    alert('Invalid Username or Password! Please try again.');
    event.preventDefault();
    window.location = "test.html";
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Just you have to add a else condition like below

if (user == storedUserName && psw == storedPassWord) {
  alert("Successfully logged in!");
  return;
}
else{
  alert('Invalid Username or Password! Please try again.');
  event.preventDefault();
  window.location = "test.html";
}

If you're not using else condition, It definitely pass the if condition and says
alert("Successfully logged in!");

but after that it gives an

alert('Invalid Username or Password! Please try again.');

So just use else condition. And then all good. Good work champ ; )

about 4 years ago · Juan Pablo Isaza Relatório

0

function validlogin() {
  var user = document.getElementById('user').value;
  var psw = document.getElementById('psw').value;

  var items = JSON.parse(localStorage.getItem('login'));

  for (let i = 0; i < items.length; i++) {

    if (items[i].username == user && items[i].password == psw)
      {

      alert("welcome");
      
      
      }
     
}
  }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda