The date range field validator checks if a Date field falls within a specific range. By default, the input format for the date in a text field is of DateFormat.SHORT and depends on user’s current locale, for example:

    • U.S. locale: M/d/yy
    • French locale: d/M/yy
Using this validator in XML or annotation as follows:

    • XML: specifying type=”date” attribute in <validator> or <field-validator> elements.
    • Annotation: specifying @DateRangeFieldValidator annotation type for setter method of the field or action method (in case of plain-validator).
NOTES: This validator does not perform date range check if the field is empty, so it’s recommended to use this validator in conjunction with the required string validator.

 

1. Struts Date Validator XML Syntax

Usage:

  • Field-validator syntax:
    <field name="fieldName">
    	<field-validator type="date">
    		<param name="param name">param value</param>
    		<message>validation error message</message>
    	</field-validator>
    </field>
     

  • Plain-validator syntax:
    <validator type="date">
    		<param name="param name">param value</param>
    		<message>validation error message</message>
    </validator> 
 

Parameters:

Parameter name

Required

Description

fieldName

 

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

min

No

The minimum date range to check. It won’t be checked if omitted.

max

No

The maximum date range to check. It won’t be checked if omitted.

minExpression

No

OGNL expression used to obtain the minimum date value.

maxExpression

No

OGNL expression used to obtain the maximum date value.

parse

No

Boolean value. If set to true, the minExpressionand maxExpression will be evaluated to obtain the minimum/maximum date. Default is true.

 

Struts Date Range Field Validator XML Examples:

  • Field-validator example:
    <field name="birthday">
    	<field-validator type="date">
    		<param name="min">1/1/1960</param>
    		<param name="max">12/31/1995</param>
    		<message>Please enter your birthday between ${min} and ${max}</message>
    	</field-validator>
    </field>
  • Checking only the minimum date:
    <field name="birthday">
    	<field-validator type="date">
    		<param name="min">1/1/1990</param>
    		<message>Your birth date must be after ${min}</message>
    	</field-validator>
    </field>
  • Using OGNL expressions for minimum and maximum dates:
    <field name="birthday">
    	<field-validator type="date">
    		<param name="minExpression">${minDate}</param>
    		<param name="maxExpression">${maxDate}</param>
    		<message>Please enter your birthday between ${min} and ${max}</message>
    	</field-validator>
    </field> 
    In this case, we it requires implementing the following methods in the action or JavaBean class as follows:

    public Date getMinDate() throws ParseException {
    	DateFormat dateFormat = new SimpleDateFormat("M/d/yy");
    	return dateFormat.parse("1/18/70");
    }
    
    public Date getMaxDate() throws ParseException {
    	DateFormat dateFormat = new SimpleDateFormat("M/d/yy");
    	return dateFormat.parse("11/22/90");
    }
     

  • Plain-validator example:
    <validator type="date">
    	<param name="fieldName">birthday</param>
    	<param name="min">1/1/1960</param>
    	<param name="max">12/31/1995</param>
    	<message>Please enter your birthday between ${min} and ${max}</message>
    </validator> 


 

2. Struts @DateRangeFieldValidator Annotation

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

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

min

No

 

The minimum date to check the field value.

minExpression

No

 

OGNL expression used to obtain the minimum date value.

max

No

 

The maximum date to check the field value.

maxExpression

No

 

OGNL expression used to obtain the maximum date value.

dateFormat

No

 

Specify the format used to parse the dates specified by the min/max parameters.

 

Struts Date Range Field Validator Annotation Examples:

  • Basic field-validator:
    @DateRangeFieldValidator(
    		min = "1/1/60",
    		max = "12/31/95",
    		message = "Please enter your birthday between ${min} and ${max}"
    		)
    public void setBirthday(Date birthday) {
    	this.birthday = birthday;
    }
     

  • Specifying i18n key for the validation error message:
    @DateRangeFieldValidator(
    		min = "1/1/60",
    		max = "12/31/95",
    		message = "Default message",
    		key = "form.validation.birthday"
    		)
    public void setBirthday(Date birthday) {
    	this.birthday = birthday;
    }
     

  • Using a custom dateFormatto parse the min/max:
    @DateRangeFieldValidator(
    		min = "01/01/1980",
    		max = "31/12/1997",
    		dateFormat = "dd/MM/yyyy",
    		message = "Please enter your birthday between ${min} and ${max}"
    		)
    public void setBirthday(Date birthday) {
    	this.birthday = birthday;
    }
     

  • Plain-validator (annotating the action method):
    @DateRangeFieldValidator(
    		type = ValidatorType.SIMPLE,
    		fieldName = "birthday",
    		min = "1/1/60",
    		max = "12/31/95",
    		message = "Please enter your birthday between ${min} and ${max} LOL"
    		)
    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