I have created a html page with a 2 different pages one for login and the other page to show after the successful login.
I have added a proper validation to the login.html page as below:
When I wanted to redirect and open 2nd html page after validating the input fields, I'm unable to open instead the input fields get cleared and the data is shown in the URL.
How can I redirect to the landing.html page which is in the same folder after validating the input fields in the login.html page
function validate(event) {
event.preventDefault();
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if (username == "username" && password == "user123") {
window.location.href = "landing.html";
} else {
alert("Invalid credentials");
}
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<div class="container">
<div class="box">
<h1>Login</h1>
<form class="form">
<label>Username</label>
<div>
<i class="fa fa-user"></i>
<input type="text" name="username" id="username" placeholder="Enter Username">
</div>
<label>Password</label>
<div>
<i class="fa fa-lock"></i>
<input type="password" name="password" id="password" placeholder="Enter Password">
</div>
<a href="#" class="forgot">Forgot Password?</a>
<input type="submit" value="Login" onclick="validate()">
</form>
</div>
</div>