I wrote a simple script for validating login form as follows:
<script type="text/javascript">
function validateLoginForm(){
var idV = document.forms["LoginForm"]["id"].value;
var pwdV = document.forms["LoginForm"]["pwd"].value;
if (idV == null || idV == "" || idV.length < 5) {
alert("user ID must be filled and of more than 4 characters!!");
id.focus();
return false;
}
if (pwdV == null || pwdV == "" || pwdV.length < 5) {
alert("Password must be filled and of more than 4 characters!!");
pwd.focus();
return false;
}
return true;
}
</script>
and call it as :
<form name="LoginForm" method="POST" onsubmit="return validateLoginForm()" action="Login" >
.
.
.
.
</form>
The problem is that javaScript gives out the alert as i set correctly but in the end the Login servlet also called automatically, which is a wrong thing..Why this happens? Any solution?
Redirect to Login happens because you have js error.
You dont define variable
idandpsw.So when you try
id.focus()orpsw.focus()– you have js error and return true by defaultSo add this code: