This article explains how to validate the form using Java with complete explanations.
Form validation
Whenever we go through web sites, we need to register ourselves to become the members of the websites. Forms consist of various fields to be filled in with the candidates to get all the details of the user.
But sometimes by mistake we provide the information in the wrong format or incomplete information that should be indicated is incorrect so that user should be able to modify or correct the information filled in by the candidates for proper registration.
So by validating the form, in other words to indicate to the user that the information filled in is incorrect (in case of wrong or incorrect information) and make it correct.
Now let's use an example of a form in which we provide various fields and we make two files, one in JSP for the user interface and another in Java form validation the form.
We have made these two files individually, one in Java and another in JSP, but within one project so that the two can be connected to each other for proper validation.
Form.jsp
- <%@page import="validation.Myform" contentType="text/html" pageEncoding="UTF-8"%>
- <jsp:useBean id="form" class="validation.Myform" scope="request">
- <jsp:setProperty name="form" property="errorMessages" value='<%= errorMap %>'/>
- </jsp:useBean>
-
- <html>
- <head>
- </head>
- <body bgcolor="green">
- <%
-
- if ("true".equals(request.getParameter("process"))) {
- %>
- <jsp:setProperty name="form" property="*" />
- <%
-
- if (form.process())
-
- {
-
- HttpSession hs=request.getSession();
- hs.setAttribute("name",(String)request.getParameter("name"));
- hs.setAttribute("user",(String)request.getParameter("user"));
- hs.setAttribute("pass",(String)request.getParameter("pass1"));
- hs.setAttribute("address",(String)request.getParameter("address"));
- hs.setAttribute("pin",(String)request.getParameter("pin"));
- hs.setAttribute("contact",(String)request.getParameter("contact"));
- hs.setAttribute("email",(String)request.getParameter("email"));
- hs.setAttribute("city",(String)request.getParameter("city"));
- hs.setAttribute("code",(String)request.getParameter("code"));
- response.sendRedirect("sendmail");
- }
- }
-
-
- %>
-
- <center>
- <form action='<%=request.getRequestURI()%>' method="POST">
- <table border="0">
- <tbody>
- <tr>
- <td>Name</td>
- <td><input type="text" name="name" value='<%=form.getName()%>'/></td>
- <td><font color="red"><%=form.getErrorMessage("name") %></font></td>
- </tr>
- <tr>
- <td>Username</td>
- <td><input type="text" name="user" value='<%=form.getUser()%>'/></td>
- <td><font color="red"><%=form.getErrorMessage("user") %></font></td>
- </tr>
- <tr>
- <td>Password</td>
- <td><input type="password" name="pass1" value='<%=form.getPass1()%>' /></td>
- <td><font color="red"><%=form.getErrorMessage("pass1") %></font></td>
- </tr>
- <tr>
- <td>Re Enter Password</td>
- <td><input type="password" name="pass2" value='<%=form.getPass2()%>' /></td>
- <td><font color="red"><%=form.getErrorMessage("pass2") %></font></td>
- </tr>
- <tr>
- <td>Address</td>
- <td><input type="text" name="address" value='<%=form.getAddress()%>' /></td>
- <td><font color="red"><%=form.getErrorMessage("address") %></font></td>
- </tr>
- <tr>
- <td>Pin Number</td>
- <td><input type="text" name="pin" value='<%=form.getPin()%>' /></td>
- <td><font color="red"><%=form.getErrorMessage("pin") %></font></td>
- </tr>
- <tr>
- <td>Mobile Number</td>
- <td><input type="text" name="contact" value='<%=form.getContact()%>' /></td>
- <td><font color="red"><%=form.getErrorMessage("contact") %></font></td>
- </tr>
- <tr>
- <td>Email ID</td>
- <td><input type="text" name="email" value='<%=form.getEmail()%>'/></td>
- <td><font color="red"><%=form.getErrorMessage("email") %></font></td>
- </tr>
- <tr>
- <td>City</td>
- <td><select name="area">
- <option>Allahabad</option>
- <option>Lucknow</option>
- <option>Varanasi</option>
- <option>Kanpur</option>
- <option>Agra</option>
- </select></td>
- </tr>
- </tbody>
- </table>
-
- <center><input type="submit" value="Register Me" /></center>
- <input type="HIDDEN" name="process" value="true"/>
-
- </form>
- </center>
- </body>
- </html>
- <%!
-
- java.util.Map errorMap = new java.util.HashMap();
- public void jspInit()
- {
- errorMap.put(Myform.ERR_NAME_BLANK, "PLEASE ENTER YOUR NAME");
- errorMap.put(Myform.ERR_USER_BLANK, "PLEASE ENTER YOUR USERNAME");
- errorMap.put(Myform.ERR_USER_LENGTH, "LENGTH SHOULD BE 5 OR MORE");
- errorMap.put(Myform.ERR_USER_EXISTS, "USERNAME ALREDY EXISTS");
- errorMap.put(Myform.ERR_PASS1_BLANK, "PLEASE ENTER YOUR PASSWORD");
- errorMap.put(Myform.ERR_PASS1_LENGTH, "LENGTH SHOULD BE 5 OR MORE");
- errorMap.put(Myform.ERR_PASS2_BLANK, "PLEASE RE ENTER YOUR PASSWORD");
- errorMap.put(Myform.ERR_PASS2_MATCH, "PASSWORD DOES NOT MATCH");
- errorMap.put(Myform.ERR_ADDRESS_BLANK, "PLEASE ENTER YOUR ADDRESS");
- errorMap.put(Myform.ERR_CONTACT_BLANK, "PLEASE ENTER YOUR MOBILE #");
- errorMap.put(Myform.ERR_CONTACT_LENGTH, "LENGTH SHOULD BE 10");
- errorMap.put(Myform.ERR_CONTACT_INVALID, "INVALID CONTACT #");
- errorMap.put(Myform.ERR_PIN_BLANK, "PLEASE ENTER YOUR PIN CODE");
- errorMap.put(Myform.ERR_PIN_LENGTH, "LENGTH SHOULD BE 6");
- errorMap.put(Myform.ERR_PIN_INVALID, "INVALID PIN CODE");
- errorMap.put(Myform.ERR_EMAIL_BLANK, "PLEASE ENTER YOUR EMAIL ID");
- errorMap.put(Myform.ERR_EMAIL_INVALID, "INVALID EMAIL ID");
- }
- %>
The contents written in <%-------%> is JSP content and apart from these there are HTML content that we are all familiar with.
Output:
You can see in the output that there are many fields like name, username, password and so on that are to be filled in by the candidates.
Now the Java file to validate the form.
We need to make the various packages for this file and we have also included the package in the Java file along with some mandatory packages.
Myform.java
- package validation;
- import java.sql.*;
- import java.util.*;
- import database.conn;
-
-
-
- public class Myform {
-
- String name="";
- String user="";
- String pass1="";
- String pass2="";
- String address="";
- String contact="";
- String pin="";
- String email="";
- String code="";
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getUser() {
- return user;
- }
-
- public void setUser(String user) {
- this.user = user;
- }
-
- public String getPass1() {
- return pass1;
- }
-
- public void setPass1(String pass1) {
- this.pass1 = pass1;
- }
-
- public String getPass2() {
- return pass2;
- }
-
- public void setPass2(String pass2) {
- this.pass2 = pass2;
- }
-
- public String getAddress() {
- return address;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
-
- public String getContact() {
- return contact;
- }
-
- public void setContact(String contact) {
- this.contact = contact;
- }
-
- public String getPin() {
- return pin;
- }
-
- public void setPin(String pin) {
- this.pin = pin;
- }
-
- public String getEmail() {
- return email;
- }
-
- public void setEmail(String email) {
- this.email = email;
- }
-
- public static final Integer ERR_NAME_BLANK = new Integer(1);
- public static final Integer ERR_USER_BLANK = new Integer(2);
- public static final Integer ERR_USER_LENGTH = new Integer(3);
- public static final Integer ERR_USER_EXISTS = new Integer(4);
- public static final Integer ERR_PASS1_BLANK = new Integer(5);
- public static final Integer ERR_PASS1_LENGTH = new Integer(6);
- public static final Integer ERR_PASS1_INVALID = new Integer(17);
- public static final Integer ERR_PASS2_BLANK = new Integer(7);
- public static final Integer ERR_PASS2_MATCH = new Integer(8);
- public static final Integer ERR_ADDRESS_BLANK = new Integer(9);
- public static final Integer ERR_PIN_BLANK = new Integer(10);
- public static final Integer ERR_PIN_LENGTH = new Integer(11);
- public static final Integer ERR_PIN_INVALID = new Integer(12);
- public static final Integer ERR_CONTACT_BLANK = new Integer(13);
- public static final Integer ERR_CONTACT_LENGTH = new Integer(14);
- public static final Integer ERR_CONTACT_INVALID = new Integer(15);
- public static final Integer ERR_EMAIL_BLANK = new Integer(16);
- public static final Integer ERR_EMAIL_INVALID = new Integer(17);
-
-
- Map errorCodes = new HashMap();
-
- Map msgMap;
- public void setErrorMessages(Map msgMap) {
- this.msgMap = msgMap;
- }
-
- public String getErrorMessage(String propName) {
- Integer code = (Integer) (errorCodes.get(propName));
- if (code == null) {
- return "";
- } else if (msgMap != null) {
- String msg = (String) msgMap.get(code);
- if (msg != null) {
- return msg;
- }
- }
- return "Error";
- }
- public boolean isValid() {
-
- errorCodes.clear();
-
- if(name.length()==0)
- {
- errorCodes.put("name", ERR_NAME_BLANK);
- }
-
- if(user.length()==0)
- {
- errorCodes.put("user",ERR_USER_BLANK);
- }
- else
- if(user.length()<5)
- {
- errorCodes.put("user",ERR_USER_LENGTH);
- }
- else
- {
-
- try
- {
- conn ob=new conn();
- Connection con=ob.c();
- Statement stm=con.createStatement();
- ResultSet rst=stm.executeQuery("select * from client where username='"+user+"'");
- if(rst.next())
- {
- errorCodes.put("user",ERR_USER_EXISTS);
- }
-
- }
- catch(Exception e)
- {}
-
- }
- if(pass1.length()==0)
- {
- errorCodes.put("pass1",ERR_PASS1_BLANK);
- }
- else
- if(pass1.length()<5)
- {
- errorCodes.put("pass1",ERR_PASS1_LENGTH);
- }
- if(pass2.length()==0)
- {
- errorCodes.put("pass2",ERR_PASS2_BLANK);
- }
- else
- if(!(pass1.equals(pass2)))
- {
- errorCodes.put("pass2",ERR_PASS2_MATCH);
- }
-
- if(address.length()==0)
- {
- errorCodes.put("address", ERR_ADDRESS_BLANK);
- }
-
- if(pin.length()==0)
- {
- errorCodes.put("pin",ERR_PIN_BLANK);
- }
- else
- if(pin.length()!=6)
- {
- errorCodes.put("pin",ERR_PIN_LENGTH);
- }
- else
- {
- try
- {
- long n1=Long.parseLong(pin);
- }
- catch(NumberFormatException e)
- {
- errorCodes.put("pin",ERR_PIN_INVALID);
- }
- }
-
- if(contact.length()==0)
- {
- errorCodes.put("contact",ERR_CONTACT_BLANK);
- }
- else
- if(contact.length()!=10)
- {
- errorCodes.put("contact",ERR_CONTACT_LENGTH);
- }
- else
- {
- try
- {
- long n1=Long.parseLong(contact);
- }
- catch(NumberFormatException e)
- {
- errorCodes.put("contact",ERR_CONTACT_INVALID);
- }
- }
- if (email.length() == 0) {
- errorCodes.put("email", ERR_EMAIL_BLANK);
- } else if (!email.matches(".+@.+\\..+")) {
- errorCodes.put("email", ERR_EMAIL_INVALID);
- }
-
- return errorCodes.size() == 0;
- }
-
- public boolean process() {
- if (!isValid()) {
- return false;
- }
-
- name="";
- user="";
- pass1="";
- pass2="";
- address="";
- contact="";
- pin="";
- email="";
- code="";
-
- errorCodes.clear();
- return true;
- }
- }
In the code above we can see the validation packages are included along with one package of a database is also included so that all the details of the user are saved in the database if the user has filled in all the fields correctly.
For this we have made various types of errors that may be done by the user. Like this we can also make more types of errors depending on our needs.
Output
The output after filling in some fields incorrectly is as follows:
In the output we can see the errors in red due to the incorrect information filled in by the candidate. The errors “length username should be 5 or more”, “mobile number should be of 10 digits” and “invalid email” are shown because the user did not write ”@”. Since we have specified the errors, the errors are shown using validation that is done here in Java.