An overview of Struts 2 field validators
Struts 2 comes with a validation framework part of which are field validators that cover a wide range of validation cases-from required field validation to regular expression validation.In this tutorial we will overview some of the build-in and most frequently used field validators.
As an example consider the case where we have a registration form with fields that require most of the build-in validators.
1) String length validator
Validates that field’s value length is between the values defined by its minLength and maxLength parameters
Parameters:
minLength:defines the minimum length of the string
maxLength: defines the maximum length of the string
Usage:
<field name="password"> <field-validator type="stringlength" > <param name="minLength">8</param> <param name="maxLength">10</param> <message>password length must be between ${minLength} and ${maxLength}</message> </field-validator> </field>
Note that in the return message we’ve used Open Graph Navigation Language (OGNL) expressions to set the minLength and maxLength part of the message
2)Required String validator
Validates that field’s value is neither null nor empty string
Parameters
trim: (true/false) trims whitespaces and its default value is true
Usage:
<field name="userName"> <field-validator type="requiredstring"> <message>User Name is required</message> </field-validator> </field>
3)Regex Validator
Regular expression validator validates that the string confoms to the regular expression declared in the “expression” parameter
Parameters:
expression: The regular expression
caseSensitive: (true/false) defines the case sensivity of the validator
trim: trims the whitespaces
Usage:
<field name="password"> <field-validator type="requiredstring"> <message key="password.required" /> </field-validator> <field-validator type="stringlength" > <param name="minLength">8</param> <param name="maxLength">10</param> <message>password length must be between ${minLength} and ${maxLength}</message> </field-validator> <field-validator type="regex"> <param name="expression">a-zA-Z</param> <message>Password must contain only letters</message> </field-validator> </field>
4) Emai validator:
Validates that field value has the propper email format -ex: [email protected]
Parameters: none
Usage:
<field name="email"> <field-validator type="email"> <message>Email Address is not valid</message> </field-validator> </field>
5)url Validator:
validates that field’s value has the proper url format – ex. http://www.SimpleCodeStuffs.com
Parameters: none
Usage:
<field name="website"> <field-validator type="url"> <message>Website address must be a valid address</message> </field-validator> </field>
6) Date validator:
verifies that the date value is between the dates defined by its Min and Max parameters
Parameters:
Min: specifies the minimum date
Max: specifies the maximum date
Usage:
<field name="birthDate"> <field-validator type="requiredstring" > <message>Birth Date is required</message> </field-validator> <field-validator type="date"> <param name="Min">1/1/1950</param> <param name="max">1/1/2100</param> <message>Birth Date must be a valid date</message> </field-validator> </field>
7) int Validator
Verifies that field’s vaule is an integer and that it is between the values defined by its Min and max parameters
Parameters:
Min: defines the minimum value that the field can have
Max: defines the maximun value that the field can have
Usage:
<field name="age"> <field-validator type="int"> <param name="Min">0</param> <param name="max">10</param> <message>Pleas enter a valid age between ${min} and ${max}</message> </field-validator> </field>