The string length field validator checks if length of a String field is within a certain range, e.g. a password must contain at least 6 characters and no more than 12 characters. Using this validator in XML or annotation as follows:

NOTES: This validator does not perform length check if the String field is empty, so it’s recommended to use this validator in conjunction with the required string validator.

 

1. Struts String Length Field Validator XML

Usage:

Parameters:

Parameter name

Required

Description

fieldName

 

Name of the field (required if using plain validator syntax).

minLength

No

An integer number specifies minimum length of the field value.

maxLength

No

An integer number specifies maximum length of the field value.

trim

No

Bolean value. If set to true, trim the field value before validating its length. Default is true.

minLengthExpression

No

OGNL expression used to obtain the minimum length value.

maxLengthExpression

No

OGNL expression used to obtain the maximum length value.

trimExpression

No

OGNL expression used to obtain the trim flag.

 

Struts String Length Field Validator XML Examples:

  

2. Struts @StringLengthFieldValidator Annotation



Usage: Annotate the String field’s setter method or action method by the @StringFieldValidatoras follows:

@StringLengthFieldValidator(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).

minLength

No

 

The minimum length to check the field value.

minLengthExpression

No

 

OGNL expression used to obtain the minimum length value.

maxLength

No

 

The maximum length to check the field value.

maxLengthExpression

No

 

OGNL expression used to obtain the maximum length value.

trim

No

true

Whether to evaluate trim the field value before validating.

trimExpression

No

 

OGNL expression used to obtain the trim flag.

 

Struts @StringLengthFieldValidator Annotation Examples:

 

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.