Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!