File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes future date validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "future date validation" Watch "future date validation" New topic

future date validation

sandeep chalishazarwala

Joined: Dec 28, 2008
Posts: 25
Hello Everybody,

I am using calendar in struts project for selecting date in dd-MMM-yy format.
Now i don't want to allow user to select future dd-MMM-yy(e.g 1/Jan/2010 onwards)
Kindly help me how to do validation in Struts using Validate method in Validator form.

Aditya Keyal
Ranch Hand

Joined: Dec 01, 2008
Posts: 71
Inside the validate method retreive the date and compare with system date. If it is a future date add an ActionError message. The date comparison would be as in basic java.

If you plan to do the same using struts-validator xml framework then it is a difficult proposition. One alternative you have is to keep a hidden variable with the current date and in the validation.xml compare the user selected date with the hidden date (current system date) and allow only if it is lesser than the current date.

- Aditya Webservices Blog
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2904

The method Calendar#compareTo() serves for this purpose, provided the property of your Action Form is of type Calendar.

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Pragna Mohapatra

Joined: Dec 11, 2008
Posts: 5
you can use the validator.xml
and for to arriave at your desired date format use the following piece of code which you can define your date format

date - validates that a field can be converted to a Date. This validation rule uses java.text.SimpleDateFormat to parse the date and optionally either a datePattern or datePatternStrict variable can be used. If no pattern is specified the default short date format is assumed. The difference between using the datePatternStrict and datePattern variables is that datePatternStrict checks additionally that the input data is the same length as the pattern specified (so for example 1/1/2004 would fail with a pattern of MM/dd/yyyy).

<field property="saledate" depends="required,date">
<arg0 key="myForm.saledate"/>

<field property="saledate" depends="required,date">
<arg0 key="sale.orderdate"/>
in the depends field you can have your own defined java script functions to validate the date.
David Newton

Joined: Sep 29, 2008
Posts: 12617

@Pragna: That solves the *format* of the date, but not the date itself, which the previous two answers did.
Pragna Mohapatra

Joined: Dec 11, 2008
Posts: 5
hi pal
we have two xml
one is validator and another validation xml
in validator for the "form' attribute date feild , in the depends field you can define a java script function.

the function may be like the following piece of code ...

function validateDateShortYear(form) {
var bValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
oDate = eval('new ' +jcv_retrieveFormName(form)+'_DateValidations() ');
for (x in oDate) {
var value = form[oDate[x][0]].value;
var datePattern = oDate[x][2]("datePatternStrict");
if ((form[oDate[x][0]].type == 'text' ||
form[oDate[x][0]].type == 'textarea') &&
(value.length > 0) &&
(datePattern.length > 0)) {
var MONTH = "MM";
var DAY = "dd";
var YEAR = "yy";
var orderMonth = datePattern.indexOf(MONTH);
var orderDay = datePattern.indexOf(DAY);
var orderYear = datePattern.indexOf(YEAR);

this way you can get the field value for the the pattern and check each field value should not be greater than the your desired value.

I agree. Here's the link:
subject: future date validation
It's not a secret anymore!