I am looking to know how to pass form values from an html from in a jsp page to a Java class when the JavaScript form validation returns true.
HTML code:
<form id="loginForm" onsubmit="return loginValidation()>
<p class="error" id="liusrError"></p>
<label for="usr">Username :</label>
<br />
<input type="text" name="liusr" id="liusr">
<br />
<p class="error" id="lipwdError"></p>
<label for="pwd">Password :</label> <br />
<input type="password" name="lipwd" id="lipwd">
<br />
<a id="fgtpwd" href="url">Forgot your password?</a>
<br />
<input type="submit" value="Log In" id="logIn">
</form>
The JavaScript:
function loginValidation(){
var pwd = document.getElementById("lipwd").value;
var usr = document.getElementById("liusr").value;
var error = false;
if(pwd == null || pwd == ""){
document.getElementById("lipwdError").innerHTML = "Please enter a valid password";
error = true;
}else{
document.getElementById("lipwdError").innerHTML = " ";
}
if(usr == null || usr==""){
document.getElementById("liusrError").innerHTML = "Please enter a valid username";
error = true;
}else{
document.getElementById("liusrError").innerHTML = " ";
}
return error;
}
EDIT:
I want to know how to use this <% if ( request.getParameter("liusr") != null ) { %> to send the values entered in my form to my Java class only when the JavaScript function loginValidation returns true.
You probably want to have your
<form>actually POST it’s contents to a URL when validation is complete:And then your Java class should be mapped to handle POST requests to
/login.