Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Date after today validation on Date/Time Field

Papote13
Level 2
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
pguerett
Correct answer by
Level 10
Level 10

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

0 Replies
pguerett
Correct answer by
Level 10
Level 10

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

radzmar
Level 10
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

Papote13
Level 2
Level 2

Thanks guys!

Is there really a difference in using javascript vs FormCalc?

pguerett
Level 10
Level 10

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

riffardjea
Level 1
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.