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:
<field name="fieldName"> <field-validator type="regex"> <param name="regex">regular expression</param> <message>validation error message</message> </field-validator> </field>
<validator type="regex"> <param name="fieldName">myField</param> <param name="regex">regular expression</param> <message>validation error message</message> </validator>
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. |
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 name="phoneNumber"> <field-validator type="regex"> <param name="regex"><![CDATA[^\+?[0-9\-]+\*?$]]></param> <message>Please enter a valid phone number</message> </field-validator> </field>
<validator type="regex"> <param name="fieldName">phoneNumber</param> <param name="regex"><![CDATA[^\+?[0-9\-]+\*?$]]></param> <message>Please enter a valid phone number</message> </validator>
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", ...)
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. |
@RegexFieldValidator( regex = "^\\+?[0-9\\-]+\\*?$", message = "Please enter a valid phone number" ) public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; }
@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; }
@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: