The regex validator validates the given string with the specified regular expression. It can be used in password, security key etc.
Parameters of regex validator
There is 4 parameters defined for regex validator.
Parameter | Description |
---|---|
fieldName | specifies the field name that is to be validated. It is required in Plain-Validator only. |
expression | specifies the regular expression. |
caseSensitive | specifies if the expression should be matched in case sensitive way. It is true bydefault. |
trim | specifies if the value should be trimmed before matching. It is true bydefault. |
Example of regex validator
- <validators>
- <!– Plain Validator Syntax –>
- <validator type=“regex”>
- <param name=“fieldName”>data</param>
- <param name=“expression”>[A-Z,a-z,0-9]{5}</param>
- <message>data must be alpha numeric of 5 digits</message>
- </validator>
- </validators>
- <validators>
- <!– Field Validator Syntax –>
- <field name=“data”>
- <field-validator type=“regex”>
- <param name=“expression”>[A-Z,a-z,0-9]{5}</param>
- <message>data must be alpha numeric of 5 digits</message>
- </field-validator>
- </field>
- </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
- <%@ taglib uri=“/struts-tags” prefix=“s” %>
- <html>
- <head>
- <STYLE type=“text/css”>
- .errorMessage{color:red;}
- </STYLE>
- </head>
- <body>
- <marquee>validation………..</marquee>
- <s:form action=“register”>
- <s:textfield name=“data” label=“Data”></s:textfield>
- <s:submit value=“register”></s:submit>
- </s:form>
- </body>
- </html>
2) Create the action class
This action class inherits the ActionSupport class and overrides the execute method.
RegisterAction.java
- package com.Kreationnext;
- import com.opensymphony.xwork2.ActionSupport;
- public class Register extends ActionSupport{
- private String data;
- public String getData() {
- return data;
- }
- public void setData(String data) {
- this.data = data;
- }
- public String execute(){
- return “success”;
- }
- }
3) Create the validation file
Here, we are using bundled validators to perform the validation.
Register-validation.xml
- <?xml version=“1.0” encoding=“UTF-8”?>
- <!DOCTYPE validators PUBLIC
- “-//OpenSymphony Group//XWork Validator 1.0.2//EN”
- “http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd”>
- <validators>
- <!–<field name=“data”>
- <field-validator type=“regex”>
- <param name=“expression”>[A-Z,a-z,0–9]{5}</param>
- <message>data must be alpha numeric of 5 digits</message>
- </field-validator>
- </field>
- –>
- <field name=“data”>
- <field-validator type=“regex”>
- <param name=“expression”>[A,a][A-Z,a-z,0–9]{5}</param>
- <message>data must be alpha numeric of 6 digits and starts with a or A</message>
- </field-validator>
- </field>
- </validators>
4) Create struts.xml
This xml file defines an extra result by the name input, and an interceptor jsonValidatorWorkflowStack.
struts.xml
- <?xml version=“1.0” encoding=“UTF-8” ?>
- <!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.1//EN” “http://struts.apache.org/dtds/struts-2.1.dtd”>
- <struts>
- <package name=“default” extends=“struts-default”>
- <action name=“register” class=“com.Kreationnext.Register”>
- <result name=“input”>index.jsp</result>
- <result>welcome.jsp</result>
- </action>
- </package>
- </struts>
5) Create view component
It is the simple jsp file displaying the information of the user.
welcome.jsp
- <%@ taglib uri=“/struts-tags” prefix=“s” %>
- Data is,<s:property value=“data”/>