MNM MNM - 1 month ago 13
HTML Question

Javascript will not move to the next webpage after user validation in a Spring Framework

I am trying to have a simple validation check before I let a user move to another part of my webpage. Don't worry this is just to set up a simple version and I would never have the password and user name as variables in the js for all to see. This is just a starting point and to test my flow of my url in my webpage.

There are two issues that I am having with my code. So when I put the right user name and pass in it works and says redirecting, but it does not redirect to the webpage. It just reloads the login page and goes no where.

The second issues is when I put in the wrong password, the attempt count never goes below 2.

Thank you for any help with this.

Here is the script that I am using

<html>
<head>
<script>
var attempt = 3; // Variable to count number of attempts.
// Below function Executes on click of login button.
function validate(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if ( username == "gus" && password == "cool"){
alert ("Login successfully");
window.location.href = "/hub/hubpage"; // Redirecting to other page.
return false;
}
else{
attempt --;// Decrementing by one.
alert("You have left "+attempt+" attempt;");
// Disabling fields after 3 attempts.
if( attempt == 0){
document.getElementById("username").disabled = true;
document.getElementById("password").disabled = true;
document.getElementById("submit").disabled = true;
return false;
}
}
}
</script>
</head>

<body>
<h2>Login Page </h2>
<a href="/hub/hubpage">Hub</a>

<br/>
<br/>
<form action = "">
<fieldset>
<legend>Login Infomration</legend>
<label>User Name :</label><br/>
<input type="text" name="username" id="username"/><br/>
<label>Password :</label><br/>
<input type="password" name="password" id="password"/><br/>
<br/>
<input type="submit" value ="Login" onclick ="validate()">
</fieldset>
</form>

</body>
</html>


The href the hubpage works fine as a link so I know it can reach there

Answer

For 1st question, change input type to button. <input type="button" value ="Login" onclick ="validate();"> For 2nd question, try attempt = attempt - 1;