Example of Regular Expression Validation

Here, we are going to create an example of regular expression validation.

Following are the steps to create an example of a regular expression:

  • Create a request page

In this step, we are going to create a request page named index.jsp for the web application.

index.jsp

  • Create the Model class

In this step, we are going to create the Model class named Customer.java, which stores the data.

Customer.java

  • Create the Controller

In this step, we are going to create a Controller class named MainCustomer.java, which returns the JSP pages.

MainCustomer.java

  • Add the entry of Controller in the web.xml

In this step, we are going to add the entry of the Controller in the web.xml.

web.xml

  • Add the entry of Model class in another XML file

In this step, we are going to add the entry of Model/ Bean in another XML file (spring-servlet.xml).

spring-servlet.xml

  • Create all the view pages

In this step, we are going to create all the view pages (JSP pages) require in the project.

customerform.jsp

customerformdata.jsp

Output

Below are given the outputs of the application:

Spring MVC Regular Expression Validation

In the above output, a link (Customer Registration Form).

Spring MVC Regular Expression Validation 1

In the above output, we have added a new field POSTAL CODE in which we have applied the @Pattern annotation, and the rest fields are the same as of the previous example.

Spring MVC Regular Expression Validation 2

The above output shows that the POSTAL CODE test field is required. We cannot pass empty values in it.

Spring MVC Regular Expression Validation 3

The above output shows that the postal code must be of 6 letters (characters and digits). But we have provided 3-digits, that’s why it is showing the error “must be of 6 char/digit”.

Spring MVC Regular Expression Validation 4

In the above output, we have provided an 11-digit postal code to the POSTAL CODE text field. That is why it is showing an error.

Spring MVC Regular Expression Validation 5

In the above output, we have provided the 6-digit postal code, which is going to pass the validation.

Spring MVC Regular Expression Validation 6

Pin It on Pinterest

Share This