Expand my Community achievements bar.

Date Field question

Avatar

Level 2

Hi there,

I'm trying to figure out what I am doing wrong. I need my date field allow entering the current date or any date in the past, not future dates.

In earlier discussions I found the code for it (the girl said it worked fine), but it's not working in my form. It's in the change event, FormCalc.

 

//xfa.host.messageBox(xfa.event.newText)

// Get the number of days from the epoch for todays date

var

todayNumber = date()

// Get the number of days from the epoch for the ending date

var

endNumber = Date2Num(xfa.event.newText, "M/D/YY")

 

//xfa.host.messageBox(Concat("Todays number is: ", todayNumber, "EndNumber is: ", endNumber))

 

if

((todayNumber - endNumber) <= 0 ) then

// Tell the user

xfa.host.messageBox("You must choose a date earlier than today!")

endif

Could you please help me?

Thank you in advance!

Maria

5 Replies

Avatar

Level 10

Hi,

this is not that complicated.

Use this FormCalc script in the layoutReady:event of your date field.

var Today = date()

var Selection = Date2Num($.formattedValue, "D/M/YY")


if (Selection > Today) then

     xfa.host.messageBox("Please enter an earlier date")

     $ = Num2Date(Today, "D/M/YY")

endif

Make shure that you use the pattern date{D/M/YY} for the displayed value.

radzmar

http://thelivecycle.blogspot.com/

Avatar

Level 2

Hi again,

I got one more question: if I need it vice versa, e.g. I need the date field allowing to enter today's date or later, not any past.

I tried to change the sign >, it works fine, but it automautically shows the current date when opening the form. Could you please help me?

Thank you,

Maria

Avatar

Level 10

Just a little amendment

var Today = date()

var Selection = Date2Num($.formattedValue, "D/M/YY")


if (Selection < Today) then

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

     $ = Num2Date(Today, "D/M/YY")

endif

radzmar

http://thelivecycle.blogspot.com/

Avatar

Level 2

Thank you radzmar!

It works OK, but when I open the form the warning message "Please enter a later date" appears... Why is it so?