The Struts validation framework provides a built-in validator that is able to validate a field against a particular regular expression: the regular expression validator. That would be useful in cases we want to make sure the value entered by the user conforms to a specific pattern, e.g. phone number or zip code. We can use this validator in either of the following forms:

  • XML: using type=”regex” attribute in <validator> or <field-validator> elements.
  • Annotation: using @RegexFieldValidator annotation type to annotate getter/setter method of the field or action method (plain-validator).
 

1. Struts Regular Expression Validator XML

Usage:

  • Field-validator syntax:
    <field name="fieldName">
    	<field-validator type="regex">
    		<param name="regex">regular expression</param>
    		<message>validation error message</message>		
    	</field-validator>
    </field>
     

  • Plain-validator syntax:
    <validator type="regex">
    	<param name="fieldName">myField</param>
    	<param name="regex">regular expression</param>
    	<message>validation error message</message>
    </validator>
 

Parameters:

Parameter name

Description

fieldName

Name of the field to validate. Required if using plain validator syntax.

regex

The regular expression which should be enclosed in a CDATA element.

caseSensitive

A boolean value specifies whether the expression should be matched against in a case-sensitive way. Default is true.

trim

A boolean value specifies whether the expression should be trimmed before matching. Default is true.

regexExpression

Defines the regular expression as an OGNL expression. This will be evaluated to String.

caseSensitiveExpression

Defines the caseSensitive parameter as an OGNL expression. This will be evaluated to boolean.

trimExpression

Defines the trim parameter as an OGNL expression. This will be evaluated to boolean.

 

Struts Regular Expression Validator XML Examples:

In the following examples, we use the regular expression validator to validate a phone number field which can accepts only these characters: numbers (0-9), +, -, and *. Hence the following regular expression:

^\\+?[0-9\\-]+\\*?$

  • Field-validator example:
    <field name="phoneNumber">
    	<field-validator type="regex">
    	    <param name="regex"><![CDATA[^\+?[0-9\-]+\*?$]]></param>
    	    <message>Please enter a valid phone number</message>
    	</field-validator>
    </field>
  • Plain-validator example:
    <validator type="regex">
    	<param name="fieldName">phoneNumber</param>
    	<param name="regex"><![CDATA[^\+?[0-9\-]+\*?$]]></param>
    	<message>Please enter a valid phone number</message>
    </validator>
 

2. Struts @RegexFieldValidator Annotation

Usage: Put the @RegexFieldValidator annotation before the field’s setter/getter method or action method (in case of using plain-validator) in the following form:

@ReqexFieldValidator(param1 = "param 1 value", param2 = "param 2 value", ...)

Parameters:

Parameter name

Required

Default value

Description

message

Yes

 

validation error message.

key

No

 

i18n key for validation error message.

messageParams

No

 

Additional parameters to customize the message.

fieldName

No

 

Specifies field name in case this validator type is plain-validator.

shortCircuit

No

false

Whether this validator is short circuit.

type

No

ValidatorType.FIELD

type of the validator: field-validator (FIELD) or plain-validator (SIMPLE).

regex

Yes

 

The regular expression.

 

Struts @RegexFieldValidator Annotation Examples:

  • Basic field-validator (annotating setter method):
    @RegexFieldValidator(
    	regex = "^\\+?[0-9\\-]+\\*?$",
    	message = "Please enter a valid phone number"
    )
    public void setPhoneNumber(String phoneNumber) {
    	this.phoneNumber = phoneNumber;
    }
  • Specifying i18n key for the message:
    @RegexFieldValidator(
    	regex = "^\\+?[0-9\\-]+\\*?$",
    	key = "form.validation.phoneNumber",
    	message = "This is the default message if the i18k key not found"
    )
    public void setPhoneNumber(String phoneNumber) {
    	this.phoneNumber = phoneNumber;
    }
  • Plain-validator (annotating the action method):
    @RegexFieldValidator(
    	type = ValidatorType.SIMPLE,
    	fieldName = "phoneNumber",			
    	regex = "^\\+?[0-9\\-]+\\*?$",
    	message = "Please enter a valid phone number"
    )
    public String execute() {
    	return SUCCESS;
    }


 

Related Struts Form Validation Tutorials:

 

Other Struts Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment