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

157
Vistas
How to stop 'submit' form when it is not valid and display error message?

when I put invalid id and password, the page just display the error message and refresh the page. Since I don't want the page to get refresh, how to stop the 'submit'.

const accounts = [
  ["myaccount", "mypassword1"],
  ["myaccount2", "mypassword2"],
];
  const event = document.getElementById("info");
  event.addEventListener("submit", (e) => {
    accounts.forEach((element) => {
      if (element[0] === id && element[1] === password) {
        pass = true;
      }
    });
    if (pass) {
      signInForm.action = "builder.html";
    } else {
      e.stopPropagation();
      document.getElementById("error").innerText = "error";
    }
  });
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try using event.preventDefault() method.

https://www.w3schools.com/jsref/event_preventdefault.asp

else {
      e.preventDefault();
      e.stopPropagation();
      document.getElementById("error").innerText = "error";
    }

This might help too - https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation

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