'Can you create a basic password validation form that redirects to another page if the password is correct?

I'm still gripping the ropes of HTML, and I was wondering if you could create a basic form that checks if the entered item is correct, then do something like send an alert or redirect to another page. I already know how to create redirects and alerts but I'm just having trouble with the password part. Thanks in advance.



Solution 1:[1]

Unfortunately with the nature of JavaScript the password will always be able to obtained from the source code, unless you link it with a backend API Request.

But here is a very simple example with the password opensesame.

function submitHandler(event) {
  const formData = new FormData(event.target);
  const password = "opensesame";
  if (formData.get("password") === password) {
    alert("You're In!");
  }
  event.preventDefault();
  return false;
}

const form = document.getElementById("form");
form.addEventListener("submit", submitHandler);
<form id="form">
  <label>Password: <input name="password" type="password"></label>
  <br><br>
  <button type="submit">Submit form</button>
</form>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ChazUK