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.
<field name="fieldName"> <field-validator type="stringlength"> <param name="param name">param value</param> <message>validation error message</message> </field-validator> </field>
<validator type="stringlength"> <param name="param name">param value</param> <message>validation error message</message> </validator>
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. |
<field name="password"> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">12</param> <message>Your password must have from ${minLength} to ${maxLength} characters</message> </field-validator> </field>
<field name="password"> <field-validator type="stringlength"> <param name="minLength">6</param> <message>Your password must have at least ${minLength} characters</message> </field-validator> </field>
<field name="password"> <field-validator type="stringlength"> <param name="minLength">12</param> <param name="maxLength">12</param> <message>Your password must contain exactly ${maxLength} characters</message> </field-validator> </field>
<field name="password"> <field-validator type="stringlength"> <param name="minLengthExpression">${minLengthValue}</param> <param name="maxLengthExpression">${maxLengthValue}</param> <message>Your password must have from ${minLength} to ${maxLength} characters</message> </field-validator> </field>
In this case, we have to implement two methods getMinLengthValue() and getMaxLengthValue() in the action or JavaBean class as follows:
public int getMinLengthValue() { return 6; } public int getMaxLengthValue() { return 12; }
<validator type="stringlength"> <param name="fieldName">password</param> <param name="minLength">6</param> <param name="maxLength">12</param> <message>Your password must have from ${minLength} to ${maxLength} characters</message> </validator>
Usage: Annotate the String field’s setter method or action method by the @StringFieldValidatoras follows:
@StringLengthFieldValidator(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). |
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. |
@StringLengthFieldValidator( minLength = "6", maxLength = "12", message = "Your password must have from ${minLength} to ${maxLength} characters" ) public void setPassword(String password) { this.password = password; }
@StringLengthFieldValidator( minLength = "6", maxLength = "12", message = "Default message", key = "form.validation.password" ) public void setPassword(String password) { this.password = password; }
@StringLengthFieldValidator( type = ValidatorType.SIMPLE, fieldName = "password", minLength = "6", maxLength = "12", message = "Your password must have from ${minLength} to ${maxLength} characters" ) public String execute() { return SUCCESS; }
Related Struts Form Validation Tutorials: