Ich fragte mich, was mit meinem Code falsch ist. Es wird angenommen, dass JSP das gesamte Feld, sei es das erforderliche Textfeldformat usw. zuerst validiert, bevor es zum Servlet geht. Ich habe den folgenden Code verwendet, um jsp selbst zu überprüfen, bevor ich das Servlet benutze. Dies funktioniert jedoch nicht, da es meinen JSP-Funktion validation() -Code nicht ausgelöst hat, ging es direkt zum Servlet.JSP validierte sich zuerst, bevor ich zum Servlet ging
<form action="RegistrationServlet" method="post" enctype="multipart/form-data" onSubmit="return validation(event);">
Der folgende Code wird meine JSP sein:
<!DOCTYPE HTML><%@page language="java"
contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Registration</title>
<script>
function validation(){
var userid =document.getElementById("userID").value;
var firstName= document.getElementById("fName").value;
var lastName = document.getElementById("lName").value;
var phoneNo= document.getElementById("phoneNo").value;
var dateOfBirth=document.getElementById("dob").value;
var displayName= document.getElementById("dName").value;
var password = document.getElementById("password").value;
var address= document.getElementById("address").value;
var interest = document.getElementById("interest").value;
var emailAddress = document.getElementById("emailAddress").value;
//var profilePic=document.getElementById("profilePic").value;
var profilePiclbl=document.getElementById("lblProfilePic").innerHTML;
alert("cc");
alert(emailAddress);
//first check all whether is it empty
//check individual whether is it empty
//else forward to next page & create session
//check the format
// forward to next page & create session
//servlet check label is it empty if not send to confirmation page
if(firstName==""){
document.getElementById("lblfName").style.color = "red";
document.getElementById("lblfName").innerHTML="Please fill in your first name.";
alert("Please check your first name.");
return false;
}
else {
document.getElementById("lblfName").innerHTML="";
}
if(lastName==""){
document.getElementById("lbllName").style.color = "red";
document.getElementById("lbllName").innerHTML="Please fill in your last name.";
alert("Please check your last name.");
return false;
}
else {
document.getElementById("lbllName").innerHTML="";
}
if(displayName==""){
document.getElementById("lbldName").style.color = "red";
document.getElementById("lbldName").innerHTML="Please fill in your display name.";
alert("Please check your display name.");
return false;
}
else {
document.getElementById("lbldName").innerHTML="";
}
if($("#dob").datepicker("getDate") === null) {
document.getElementById("lblDOB").style.color = "red";
document.getElementById("lblDOB").innerHTML="Please fill in your date of birth.";
return false;
}
else {
document.getElementById("lblDOB").innerHTML="";
}
if(phoneNo==""){
document.getElementById("lblphoneNo").style.color = "red";
document.getElementById("lblphoneNo").innerHTML="Please fill in your phone number.";
alert("Please check your phone number.");
return false;
}
else {
document.getElementById("lblphoneNo").innerHTML="";
}
if(userid==""){document.getElementById("lblUserID").style.color = "red";
document.getElementById("lblUserID").innerHTML="Please fill in your user ID.";
alert("Please check your user ID.");
return false;
}
else {
document.getElementById("lblUserID").innerHTML="";
}
if(password==""){
document.getElementById("lblPassword").style.color = "red";
document.getElementById("lblPassword").innerHTML="Please fill in your password.";
alert("Please check your password.");
return false;
}
else {
document.getElementById("lblPassword").innerHTML="";
}
if(emailAddress==""){
document.getElementById("lblemailAddress").style.color = "red";
document.getElementById("lblemailAddress").innerHTML="Please fill in your email address.";
alert("Please check your email address.");
return false;
}
else {
document.getElementById("lblemailAddress").innerHTML="";
}
if(address==""){
document.getElementById("lblAddress").style.color = "red";
document.getElementById("lblAddress").innerHTML="Please fill in address.";
alert("Please check address.");
return false;
}
else {
document.getElementById("lblAddress").innerHTML="";
}
if(interest==""){
document.getElementById("lblInterest").style.color = "red";
document.getElementById("lblInterest").innerHTML="Please fill in your interest.";
alert("Please check your interest.");
return false;
}
else {
document.getElementById("lblInterest").innerHTML="";
}
if(firstName!="" || lastName!="" || userid!="" || phoneNo!="" || dateOfBirth!="" || displayName!="" || password!="" || address!=""|| interest!=""|| emailAddress!=""){
var letters = /^[A-Za-z]+$/;
var numbers = /^\d{8}$/;
var re = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(!firstName.match(letters)|| !lastName.match(letters)){
if (!firstName.match(letters)){
document.getElementById("lblfName").style.color = "red";
document.getElementById("lblfName").innerHTML="Please check your spelling of your first name.";
return false;
}else{
document.getElementById("lblfName").innerHTML="";
}
if(!lastName.match(letters)){
document.getElementById("lbllName").style.color = "red";
document.getElementById("lbllName").innerHTML="Please check your spelling of your last name.";
return false;
}else{
document.getElementById("lbllName").innerHTML="";
}
}
//mid
else{
document.getElementById("lbllName").innerHTML="";
document.getElementById("lblfName").innerHTML="";
}
if(!phoneNo.match(numbers)){
document.getElementById("lblphoneNo").style.color = "red";
document.getElementById("lblphoneNo").innerHTML="Please check your phone number.";
return false;
}else {
document.getElementById("lblphoneNo").innerHTML="";
}
if (!emailAddress.match(re)) {
document.getElementById("lblemailAddress").style.color = "red";
document.getElementById("lblemailAddress").innerHTML="Please check your email address.";
return false;
}else{
document.getElementById("lblemailAddress").innerHTML="";
}
if (profilePiclbl!="") {
return false;
}else{
document.getElementById("lblProfilePic").innerHTML="";
return true;
}
// $("#passRegi").val("success");
//alert($("#passRegi").val());
}
} </script>
<form action="RegistrationServlet" method="post" enctype="multipart/form-data" onSubmit="return validation(event);">
Registration
<table>
<tr>
<td>First Name:</td>
<td> <input type="text" name="fName" id="fName"></td>
<td> <label for="lblfName" id="lblfName" ></label></td>
</tr>
<tr>
<td>Last Name :</td>
<td> <input type="text" name="lName" id="lName"></td>
<td> <label for="lblDOB" id="lbllName"></label></td>
</tr>
<tr>
<td>Display Name :</td>
<td> <input type="text" name="dName" id="dName"></td>
<td> <label for="lblDOB" id="lbldName"></label></td>
</tr>
<tr>
<td>Date Of Birth :</td>
<td> <input type="text" name="dob" id="dob"></td>
<td> <label for="lblDOB" id="lblDOB"></label></td>
</tr>
<tr>
<td>Phone Number:</td>
<td> <input type="text" name="phoneNo" id="phoneNo"></td>
<td> <label for="lblphoneNo" id="lblphoneNo"></label></td>
</tr>
<tr>
<td>Email Address:</td>
<td> <input type="text" name="emailAddress" id="emailAddress"></td>
<td><label for="lblemailAddress" id="lblemailAddress"><div style="color: #FF0000;">${errorMessage}</div></label></td>
</tr>
<tr>
<td>Password:</td>
<td> <input type="password" name="password" id="password" onkeyup="CheckPasswordStrength(this.value)"></td>
<td> <label for="lblPassword" id="lblPassword"></label></td>
<td><label for="password_strength" id="password_strength"></label></td>
</tr>
<tr>
<td>Address:</td>
<td> <input type="text" name="address" id="address"></td>
<td> <label for="lblAddress" id="lblAddress"></label></td>
</tr>
<tr>
<td>Profile Picture:</td>
<td> <input type="file" name="profilePic" id="profilePic" accept="image/*" onChange="previewFile()"></td>
<td> <label for="lblProfilePic" id="lblProfilePic"></label></td>
</tr>
<tr>
<td>Interest:</td>
<td> <input type="text" name="interest" id="interest"></td>
<td> <label for="lblInterest" id="lblInterest"></label></td>
</tr>
</table>
<input type="submit" value="Submit" onClick="validation();">
</form>
</body>
</html>
Ohh vielen Dank, ich habe diesen Fehler nicht erkannt. – Qccccc