Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Date after today validation on Date/Time Field

Avatar

Level 2

I have a field in a form set as date. I am trying to validate the date entered, has to actually be a date AND equal or after today, if not stay on the field until it is corrected.

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

Use FormCalc for this it is easier to do date arithmetic in that language. You would do this on the exit event of the field (that way the user has already chosen a date). There is a function called (Date2Num) that will take the date passed to it and convert it to the number of days that have elapsed from a specific time (called an epoch). So you woudl have to het todays number then get the number of the date the user chose and do a simple test to see if todays number is greater than the current one. Something like this:

//get the number for the selected date

var selectedNum = Date2Num($.rawValue, "YYYY-MM-DD")

//the Date() function returns the number of days from the epoch for the current date (system time)

if ( selectedNum <= Date()) then

    //its earlier ..let the user know

xfa.host.messageBox("Please enter a date later than todays date!")

// put the cursor back in the Date selection field

xfa.host.setFocus("DateTimeField1")

endif

This assumes that your Date selection field is called DateTimeField1

Paul

View solution in original post

5 Replies

Avatar

Correct answer by
Former Community Member

Use FormCalc for this it is easier to do date arithmetic in that language. You would do this on the exit event of the field (that way the user has already chosen a date). There is a function called (Date2Num) that will take the date passed to it and convert it to the number of days that have elapsed from a specific time (called an epoch). So you woudl have to het todays number then get the number of the date the user chose and do a simple test to see if todays number is greater than the current one. Something like this:

//get the number for the selected date

var selectedNum = Date2Num($.rawValue, "YYYY-MM-DD")

//the Date() function returns the number of days from the epoch for the current date (system time)

if ( selectedNum <= Date()) then

    //its earlier ..let the user know

xfa.host.messageBox("Please enter a date later than todays date!")

// put the cursor back in the Date selection field

xfa.host.setFocus("DateTimeField1")

endif

This assumes that your Date selection field is called DateTimeField1

Paul

Avatar

Level 10

You can use FormCalc for this purpose.

This script in the exit:Event will warn the users, if the date is before todays date.

Oh, Paul was faster than me

Avatar

Level 2

Thanks guys!

Is there really a difference in using javascript vs FormCalc?

Avatar

Former Community Member

You will get to the same place ....doing date calculations in Javascript will require more statements than what you see in FormCalc...so I gies it comes down to a matter of preference.

Paul

Avatar

Level 1

Hello Everyone,

I know this is really late, but I don't get a warning if I pick an earlier date. What could I be doing in correctly? I literally copied and pasted. If your code still valid for 2017's pro version?

Thanks.

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----