• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

future date validation

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Regards
Sandeep
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The method Calendar#compareTo() serves for this purpose, provided the property of your Action Form is of type Calendar.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"/>
<var><var-name>datePattern</var-name><var-value>MM/dd/yyyy</var-value></var>
</field>


<field property="saledate" depends="required,date">
<arg0 key="sale.orderdate"/>
<var><var-name>datePatternStrict</var-name><var-value>MM/dd/yyyy</var-value></var>
</field>
in the depends field you can have your own defined java script functions to validate the date.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Pragna: That solves the *format* of the date, but not the date itself, which the previous two answers did.
 
Pragna Mohapatra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

reply
    Bookmark Topic Watch Topic
  • New Topic