Hi,

We were asked to make a subscription type of form. Details needed are username, password, first name, last name, etc. My problem has something to do with the password validation. when i feel in the password field and the re-type password field, both of them should be checked. If both are the same, it will be redirected to a "success page" after clicking the submit button. But if both passwords don't match, a short "error page" should be included and displayed at the bottom of the same page after clicking the submit button. I can't get it to include the short error page on the same page after clicking the submit button. I am using "IBM RAD" for this. Here's what I've got so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Main Page</title>
</head>
<body>
<form name="frm" action="exer02-b.jsp" method="post">
	Please fill in the blanks below:<hr>
	<p>Username: <input type="text" name="user" size="20"></p>
	<p>Password: <input type="password" name="pass1" size="20"></p>
	<p>Re-type password: <input type="password" name="pass2" size="20"></p><hr>	
	<p>First name: <input type="text" name="firstname" size="20"></p>
	<p>Last name: <input type="text" name="lastname" size="20"></p>	
	<p>Gender: <input type="radio" name="gender" checked>female <input type="radio"
		name="gender">male</p>	
	<input type="reset" name="res" value="Reset"> <input type="submit" name="sub" value="Submit">
	<% if ( !("pass2").equals("pass2") ) { %>
		<%@ include file="errorpage.html" %>onClick
	<%} %>	
</form>
</body>
</html>

I would appreciate anyone that would kindly help me with this problem. Thanks! :)

Recommended Answers

All 8 Replies

This is java:

if ( !("pass2").equals("pass2") ) {

}

Try to run that code and see what happens. The "pass2" string will always be equal to "pass2" string.
You write: "pass2".equals("pass2") . Those are fixed values and it is always true. And the ! will always make it false.

You need to get values from request. When you submit to a page use these methods to get the values from request.

<%
String password1 = request.getParameter("pass1"); // pass1 is the name of the input tag
String password2 = request.getParameter("pass2");

// check if the page has been submitted:
if (password1!=null && password2!=null) {
    
   // check if they are equal:
   if (!password1.equals(password2)) {
%>
     <%@ include file="errorpage.html" %>
<%
   }
}
%>

The first time you go to that page for the user to enter the values, the request is empty, so the password1, password2 would be null and the validation will not take place. But when you submit, you will send the pass1, pass2 values of the input fields and you will do the validation. I trust that exer02-b.jsp is the file that you posted. Remember the page that you enter at the action attribute of the form is where the values are sent.

This is java:

if ( !("pass2").equals("pass2") ) {

}

Try to run that code and see what happens. The "pass2" string will always be equal to "pass2" string.
You write: "pass2".equals("pass2") . Those are fixed values and it is always true. And the ! will always make it false.

You need to get values from request. When you submit to a page use these methods to get the values from request.

<%
String password1 = request.getParameter("pass1"); // pass1 is the name of the input tag
String password2 = request.getParameter("pass2");

// check if the page has been submitted:
if (password1!=null && password2!=null) {
    
   // check if they are equal:
   if (!password1.equals(password2)) {
%>
     <%@ include file="errorpage.html" %>
<%
   }
}
%>

The first time you go to that page for the user to enter the values, the request is empty, so the password1, password2 would be null and the validation will not take place. But when you submit, you will send the pass1, pass2 values of the input fields and you will do the validation. I trust that exer02-b.jsp is the file that you posted. Remember the page that you enter at the action attribute of the form is where the values are sent.

I tried your suggestion and everything worked out well. The only problem I have left right now is how to make it go to "successpage.jsp" if both passwords inputted on the "mainpage.jsp" are the same.

here is my "mainpage.jsp":

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Main Page</title>
</head>
<body>
<h1 align="center"><b>Registration Form</b></h1><hr>
<form onclick="submit">
	<p>Username: <input type="text" name="user" size="20"></p>
	<p>Password: <input type="password" name="pass1" size="20"></p>
	<% String pswrd1 = request.getParameter("pass1"); %>	
	<p>Re-type password: <input type="password" name="pass2" size="20"></p>	
	<% String pswrd2 = request.getParameter("pass2"); %>
	<hr>				
	<p>First name: <input type="text" name="firstname" size="20"></p>
	<p>Last name: <input type="text" name="lastname" size="20"></p>	
	<p>Gender: <input type="radio" name="gender" value="female">female
	<input type="radio" name="gender" value="male">male</p>			
	<input type="reset" name="res" value="Reset">
	<input type="submit" name="sub" value="Submit">	
	<% if (pswrd1!=null && pswrd2!=null) {	 
		if (!pswrd1.equals(pswrd2)) { %>
				<%@ include file="errorpage.html" %>						
	 		<%}} %>	 			
</form>
</body>
</html>

here is my "successpage.jsp":

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@page
	language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Summary Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
	"Summary Page"<hr>
	<% String uname = request.getParameter("user"); %>
	<p>USERNAME: <%= uname %></p>
	<% String pas2 = request.getParameter("pass2"); %>
	<p>PASSWORD: <%= pas2 %></p><br>
	<% String fname = request.getParameter("firstname"); %>	
	<p>FIRST NAME: <%= fname %></p>
	<% String lname = request.getParameter("lastname"); %>
	<p>LAST NAME: <%= lname %></p>
	<% String gend = request.getParameter("gender"); %>
	<p>GENDER: <%= gend %></p>
	<% String num = request.getParameter("age"); %>	
</body>
</html>

here is my "errorpage.html":

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Error Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<hr>ERROR!<br><br><br>
Both passwords must be the same.<hr>
</body>
</html>

You can use this:
<jsp:forward page="successpage.jsp" />

<% 
if (pswrd1!=null && pswrd2!=null) {	 
  if (!pswrd1.equals(pswrd2)) { 
%>
     <%@ include file="errorpage.html" %>
<%
  } else {
%>
     <jsp:forward page="successpage.jsp" />
<%
  }
} 
%>

You can use this:
<jsp:forward page="successpage.jsp" />

<% 
if (pswrd1!=null && pswrd2!=null) {	 
  if (!pswrd1.equals(pswrd2)) { 
%>
     <%@ include file="errorpage.html" %>
<%
  } else {
%>
     <jsp:forward page="successpage.jsp" />
<%
  }
} 
%>

Thanks! I'll give this a try and let you know if all goes well. =)

You can use this:
<jsp:forward page="successpage.jsp" />

<% 
if (pswrd1!=null && pswrd2!=null) {	 
  if (!pswrd1.equals(pswrd2)) { 
%>
     <%@ include file="errorpage.html" %>
<%
  } else {
%>
     <jsp:forward page="successpage.jsp" />
<%
  }
} 
%>

...just out of curiosity, is there an alternative way (besides the one above) to display the "successpage.jsp" if both passwords are the same?

You can use this:
<jsp:forward page="successpage.jsp" />

<% 
if (pswrd1!=null && pswrd2!=null) {	 
  if (!pswrd1.equals(pswrd2)) { 
%>
     <%@ include file="errorpage.html" %>
<%
  } else {
%>
     <jsp:forward page="successpage.jsp" />
<%
  }
} 
%>

Thanks for the code. It works. However I have another problem. I want it to output an error message if i did not fill in any of the blanks nor selected a gender.
I tried the following code for example:

<% if ( username.equals("") || firstname.equals("") ) { %>
   ERROR! Input missing fields.
<% } else { %>
<jsp:forward page="successpage.jsp" />
<%}%>

It always gives me an error.

Also, if for example I want to give an error message if I inputed a number(s) in the name fields.

I would really appreciate the help. Thanks so much! =)

You must always check if those values are null or not.
When you first go to that page, they are null, since you didn't submit anything.
After you check that they are not null, then do whatever validations you want.
I would suggest to use only one, the user for example. If it is not null, it means that the others were given as well.
Also for the radio button if don't select anything it submits null, so you can use that to check if the user hasn't entered anything. And add the value attribute to the radio button.


And lastly the best way to do those kinds of validations (if the user has entered empty values) use javascript:
http://www.w3schools.com/default.asp

You must always check if those values are null or not.
When you first go to that page, they are null, since you didn't submit anything.
After you check that they are not null, then do whatever validations you want.
I would suggest to use only one, the user for example. If it is not null, it means that the others were given as well.
Also for the radio button if don't select anything it submits null, so you can use that to check if the user hasn't entered anything. And add the value attribute to the radio button.


And lastly the best way to do those kinds of validations (if the user has entered empty values) use javascript:
http://www.w3schools.com/default.asp

I'm not very in making if else statements using java codes.
Will it work it is written this way?

<% if (pswrd1!=null && pswrd2!=null) {
      if (!pswrd1.equals(pswrd2)) { %>
         <%@ include file="errorpage.html" %>
      <% } else { %>
         <jsp:forward page="successpage.jsp" />
<% }} else if (usrname==null || pswrd1==null || pswrd2== null || frstname==null || lastname==null || gender==null) { %>
         ERROR! Please completely fill out the form.
      <% } else { %>
         <jsp:forward page="successpage.jsp" />
<% } %>

I remember trying a code similar to the one above that didn't give me an error. When I open the mainpage.jsp, it doesn't wait for me to click the submit button on that page but instead it directly forwards me to successpage.jsp. How can this be prevented from happening?

Also, if I don't want numbers to be inputted in the text fields how do I code this? Do I write all the numbers individually from 0-9 like the example below:

<% if (pswrd1!=null && pswrd2!=null) {
      if (!pswrd1.equals(pswrd2)) { %>
         <%@ include file="errorpage.html" %>
      <% } else { %>
         <jsp:forward page="successpage.jsp" />
<% }} else if (firstname=="0" || firstname=="1" || firstname=="2" || ....etc...) { %>
         ERROR! Only letters are allowed.
      <% } else { %>
         <jsp:forward page="successpage.jsp" />
<% } %>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.