Expand my Community achievements bar.

SOLVED

Dates to & From

Avatar

Level 3

Eeek, I'm bewildered...

I have several fields in my form requiring To and From travelling dates.  I've set them up as date fields with date choosers and that's all working fine.

However, I want a JavaScript script (although Formcalc would also do of course) that will return an error message if the 'Return' date entered is prior to the initial 'Departure' date.

So put simply if:

dateReturnFld is before dateDepartFld then

     xfa.host.messagebox("You can't return before you get there!");

I've searched through this forum, searched through tutorials and W3C pages and have found a variety of options, none that seem to directly address what I need, either that or I simply don't get it.

Can anyone please help?

Thanks,

Peta

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi Peta

Using formCalc on the exit event of the return date field the following script seems to work. It first checks to see if a departure date has been entered--if not  it tells the user to enter a departure date, clears the return date field and sets the focus on the departure date field. If a departure date has been entered then it checks to see the return date is before the departure date. If it is, then it clears the return date and tells the user "You can't return before you get there!"

    if(hasValue(DepartureDateTimeField) == 0)then
        xfa.host.messageBox("Please enter departure date")
        xfa.host.resetData("form1.Subform1.ReturnDateTimeField")
        xfa.host.setFocus("DepartureDateTimeField")
    elseif(DateTimeField1 > $) then
        xfa.host.resetData("form1.Subform1.ReturnDateTimeField")
        xfa.host.messageBox("You can't return before you get there!")
    endif

If your planning on determining the number of days between the dates, then you'll need to use the Date2Num() function--but I'm having trouble making it work! If your not planning on determining the number of days between the dates, then the script above should be enough.

I hope this helps!

Stephen

View solution in original post

2 Replies

Avatar

Correct answer by
Level 7

Hi Peta

Using formCalc on the exit event of the return date field the following script seems to work. It first checks to see if a departure date has been entered--if not  it tells the user to enter a departure date, clears the return date field and sets the focus on the departure date field. If a departure date has been entered then it checks to see the return date is before the departure date. If it is, then it clears the return date and tells the user "You can't return before you get there!"

    if(hasValue(DepartureDateTimeField) == 0)then
        xfa.host.messageBox("Please enter departure date")
        xfa.host.resetData("form1.Subform1.ReturnDateTimeField")
        xfa.host.setFocus("DepartureDateTimeField")
    elseif(DateTimeField1 > $) then
        xfa.host.resetData("form1.Subform1.ReturnDateTimeField")
        xfa.host.messageBox("You can't return before you get there!")
    endif

If your planning on determining the number of days between the dates, then you'll need to use the Date2Num() function--but I'm having trouble making it work! If your not planning on determining the number of days between the dates, then the script above should be enough.

I hope this helps!

Stephen

Avatar

Level 3

Genius!

Works a treat.

Thanks so much Stephen.