Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda