I'm trying to create a JS account system with HTML forms, but my script either won't run, or doesn't work. I've looked through the code many times and make changes, but nothing ever works. Can someone help me? Also, this system has one public password, but I could change it to be passwords for everyone. This isn't published, it is just a file that I have, and it would be on two different files, but for this I put them on the same HTML file.
<form name="login" onsubmit="login()">
<fieldset style="width:300px">
<legend>Login</legend>
<label for="username">Username:</label>
<br>
<input type="text"
name="username"
required>
<br><br>
<label for="password">Password:</label>
<br>
<input type="text"
name="password"
required>
<br><br>
<input type="submit"
name="submit">
</fieldset>
</form>
<script>
function login() {
let usernameinput = document.forms["login"]["username"].value;
let passwordinput = document.fomrs["login"]["password"].value;
var users = ["user123", "otheruser123"];
if (usernameinput == users) {
if (passwordinput == "password123") {
alert("Logged in");
window.location.replace("home.html");
} else {
alert("Incorrect username and/or password");
}
} else {
alert("Incorrect username and/or password");
}
}
</script>
I've tried changing it so that instead of two if functions, there is just one with both inputs and an and operator like this:
if (usernameinput == users && passwordinput == "password123") {
alert("Logged in");
window.location.replace("home.html");
} else {
alert("Incorrect username and/or password");
}
I have also tried just putting in usernames instead of the users
variable like this:
if (usernameinput == "user123" || usernameinput == "otheruser123" && passwordinput ==
"password123") {
alert("Logged in");
window.location.replace("home.html");
}
The last thing I tried was instead of the usernameinput
and passwordinput
variables, I tried using document.login.username.value
or document.login.password.value
like this:
if (document.login.username.value == "user123" && document.login.password.value ==
"password123") {
alert("Logged in");
window.location.replace("home.html");
}
With this, I also tried it with two if functions. I have no idea why none of this is working.