Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Regex


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

  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. </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. </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. </body>
  15. </html>

2) Create the action class

This action class inherits the ActionSupport class and overrides the execute method.

RegisterAction.java

  1. package com.Kreationnext;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class Register extends ActionSupport{
  4. private String data;
  5. public String getData() {
  6.     return data;
  7. }
  8. public void setData(String data) {
  9.     this.data = data;
  10. }
  11. public String execute(){
  12.     return “success”;
  13. }
  14. }

3) Create the validation file

Here, we are using bundled validators to perform the validation.

Register-validation.xml

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

4) Create 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. <package name=“default” extends=“struts-default”>
  5. <action name=“register” class=“com.Kreationnext.Register”>
  6. <result name=“input”>index.jsp</result>
  7. <result>welcome.jsp</result>
  8. </action>
  9. </package>
  10. </struts>

5) Create view component

It is the simple jsp file displaying the information of the user.

welcome.jsp

  1. <%@ taglib uri=“/struts-tags” prefix=“s” %>
  2. Data is,<s:property value=“data”/>

Comments

comments