Struts2 Regex Validation Example

The regex validator approves the given string with the predefined ordinary articulation. It very well may be utilized in secret phrase, security key etc.

Parameters of regex validator

There is 4 parameters characterized for regex validator.

ParameterDescription
fieldNamespecifies the field name that will be approved. It is required in Plain-Validator only.
expressionspecifies the ordinary expression.
caseSensitivespecifies if the articulation ought to be coordinated in the event that touchy way. It is genuine bydefault.
trimspecifies if the esteem ought to be cut before coordinating. It is genuine bydefault.

Example of regex validator

  1. <validators>  
  2.  <!-- Plain Validator Syntax -->  
  3.           <validator type="regex">  
  4.               <param name="fieldName">data</param>  
  5.               <param name="expression">[A-Z,a-z,0-9]{5}</param>  
  6.           <message>data must be alpha numeric of 5 digits</message>  
  7.           </validator>  
  8.             
  9. </validators>  
  1. <validators>  
  2.     <!-- Field Validator Syntax -->  
  3.          <field name="data">  
  4.         <field-validator type="regex">  
  5.           <param name="expression">[A-Z,a-z,0-9]{5}</param>  
  6.           <message>data must be alpha numeric of 5 digits</message>  
  7.         </field-validator>  
  8.     </field>  
  9.   
  10. </validators>  

Full example of regex validator

1) Create index.jsp for input

This jsp page creates a form using struts UI tags. It receives name, password and email id from the user.

index.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2. <html>  
  3. <head>  
  4. <STYLE type="text/css">  
  5. .errorMessage{color:red;}  
  6. </STYLE>  
  7. </head>  
  8. <body>  
  9. <marquee>validation...........</marquee>  
  10. <s:form action="register">  
  11. <s:textfield name="data" label="Data"></s:textfield>  
  12. <s:submit value="register"></s:submit>  
  13. </s:form>  
  14.   
  15. </body>  
  16. </html>  

2) Create the action class

This activity class acquires the ActionSupport class and supersedes the execute method.

RegisterAction.java
  1. package com.javacodegeeks;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class Register extends ActionSupport{  
  6. private String data;  
  7.   
  8. public String getData() {  
  9.     return data;  
  10. }  
  11.   
  12. public void setData(String data) {  
  13.     this.data = data;  
  14. }  
  15.   
  16. public String execute(){  
  17.     return "success";  
  18. }  
  19.   
  20. }  

3) Create the approval file

Here, we are utilizing packaged validators to play out the validation.

Register-validation.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3.   <!DOCTYPE validators PUBLIC   
  4.         "-//OpenSymphony Group//XWork Validator 1.0.2//EN"   
  5.         "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  
  6.   
  7.         <validators>  
  8.           
  9.         <!--<field name="data">  
  10.         <field-validator type="regex">  
  11.         <param name="expression">[A-Z,a-z,0-9]{5}</param>  
  12.         <message>data must be alpha numeric of 5 digits</message>  
  13.         </field-validator>  
  14.         </field>  
  15.           
  16.         -->  
  17.         <field name="data">  
  18.         <field-validator type="regex">  
  19.         <param name="expression">[A,a][A-Z,a-z,0-9]{5}</param>  
  20.         <message>data must be alpha numeric of 6 digits and starts with a or A</message>  
  21.         </field-validator>  
  22.         </field>   
  23.           
  24.         </validators>  

4) Create struts.xml

This xml document characterizes an additional outcome by the name input, and an interceptor jsonValidatorWorkflowStack.

struts.xml

This xml file defines an extra result by the name input, and an interceptor jsonValidatorWorkflowStack.

struts.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4.   
  5. <package name="default" extends="struts-default">  
  6. <action name="register" class="com.javacodegeeks.Register">  
  7. <result name="input">index.jsp</result>  
  8. <result>welcome.jsp</result>  
  9. </action>  
  10.   
  11. </package>  
  12.   
  13. </struts>      





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition