Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
189
rated 0 times [  193] [ 4]  / answers: 1 / hits: 6190  / 4 Years ago, wed, november 25, 2020, 12:00:00

So I am using an if else statement for a login form and once you enter the correct login, it should redirect you to another HTML page, but if the information is incorrect, the page just refresh.


function myFunction() {


var email = document.getElementById("email").value;
var password = document.getElementById("password").value;

if(email == "[email protected]" && password == "user")
{
window.location.replace("upload.html");

}
else {
alert("Invalid information");
return ;
}

}




    <form id="form" action="" method="GET">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" id="email" class="form-control" aria-describedby="emailHelp" >
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" id="password" class="form-control" >
</div>
<button type="submit" class="btn btn-outline-success btn-lg shadow" onClick="myFunction()">Submit</button>

</form>

More From » html

 Answers
2

first of all, using Javascript to Authenticate a user is a terrible idea, simply because your data is ALL in the client-side.


The issue with your code is that you do not have a Function, which means you are not really doing anything in your HTML you are calling


onClick="myFunction()

but it does not exist in your JS...




This should work:



function auth() {
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;

if (email === "[email protected]" && password === "user") {
window.location.replace("./upload.html");
// alert("You Are a ADMIN");

} else {
alert("Invalid information");
return;
}

}


<form id="form" action="" method="GET">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" id="email" class="form-control" aria-describedby="emailHelp" >
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" id="password" class="form-control" >
</div>
<button type="submit" class="btn btn-outline-success btn-lg shadow" onClick="auth()">Submit</button>

</form>

Regards,


[#2229] Monday, November 23, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
havenbilliec

Total Points: 324
Total Questions: 106
Total Answers: 94

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;