Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

How can I prevent the entry of a future date in my date field?

Avatar

Former Community Member
Hi,



I have a date field in my form (created w/ Adobe Lifecycle Designer v8.0) and I need to prevent users from entering a future date (current or past date is okay). Ideally, if a future date is entered, an error message pops up and tells the person that a future date is not acceptable. Does anyone know how to do this or have any suggestions to try?



Thanks,

Annie
2 Replies

Avatar

Level 5
Hope the following JS code helps you..........

var checkDate = isCurrOrPastDate(this.rawValue)

if (checkDate == true) {

//xfa.host.messageBox("This date is valid current or past date.");

}else {

xfa.host.messageBox("Date can not be a future date");

xfa.host.setFocus(this.somExpression); //set focus back to the data field.

}



function isCurrOrPastDate(dateStr) {

var dto = dateStr;

var iLen1 = String(dto).length;

var DD = dto.substring(iLen1, iLen1 - 2);

DD = DD.replace(/^\s*|\s*$/g,"");

//xfa.host.messageBox(""+DD);

var MM = dto.substring(5,7)

var YYYY = dto.substring(0,4);

//xfa.host.messageBox(""+DD+" "+MM+" "+YYYY);

//return false;

var date1 = new Date(MM+"/"+DD+"/"+YYYY);//new Date(month+"/"+day+"/"+year);

//xfa.host.messageBox(""+date1);

var today = new Date();

var ddf = dateDifference(today,date1);

//xfa.host.messageBox("ddf: "+ddf);

if (ddf <= 0) {

//xfa.host.messageBox("This date is valid current or past date.");

return true; // date is valid

}

//xfa.host.messageBox("Date can not be a future date");

return false; // date is not valid

}



function dateDifference(strDate1,strDate2){

datDate1= Date.parse(strDate1);

datDate2= Date.parse(strDate2);

//app.alert(""+(datDate2-datDate1)/(24*60*60*1000));



return (datDate2-datDate1)/(24*60*60*1000);

}

Avatar

Level 2

hi Annie,

place the below code in the exit event of the date field and script language should be FormCalc

if

($.rawValue <> null )

then

var

num = IsoDate2Num($.rawValue)-Date()

if

( num > 0 )

then

$host.messageBox("Invalid Date and cannot be Future Date")

$.rawValue

= ""

$host.setFocus("your field path")

endif

endif

Note : your field path for example your date field name is futuredate under subform name body then place body.futuredate.

if you want to resrict not to enter past date then compare num with < 0 or to current date then compare num with = 0.

Thanks,

Madhu.