Skip to main content
Inspiring
March 31, 2017
Answered

Second Date should come After First Date - Only works sometimes

  • March 31, 2017
  • 1 reply
  • 2199 views

Hi,

Using Adobe Acrobat XI Pro - Forms

Can someone please help me with my code for these date fields?  I had some pretty simple code to compare two dates, but apparently it is too simple.

Sometimes it works, and sometimes it doesn't work. 

I am trying to say: If the "From Date" is newer than the "To Date," then give an alert, and have the person correct the "To Date."   (Because the "To Date" should come after the "From Date.")

I am trying to do this for two different types of date formats.  I have the date formats set in the field properties of the date fields.

One set of date fields is using mm/yyyy, and the other set of date fields is using mm/dd/yyyy.

The code is located in the Second Date Field properties, on the Action Tab, using "On Blur/Run a Javascript."

Here is my code:

var fd = this.getField("FromDate1").value;
var sd = this.getField("ToDate1").value;
if (fd != "" && fd > sd) {
    app.alert ("From Date must come before To Date.");

//Below resets the value of the 2nd Date, but moves on to next field

    this.getField("ToDate1").value = "";

//Below sets the focus back to the 2nd Date, so you can try again

    this.getField("ToDate1").setFocus();

}

Here is a link to my form on Dropbox:

Dropbox - SampleForm.pdf

Thank you very much.

This topic has been closed for replies.
Correct answer try67

No, that's not true. You can use any format you want, as long as you take it into account in your code.

And since this is a validation script you should use it as such, not as an On Blur event.


You can use this code as the custom Validation script of ToDate1:

if (event.value) {

    var startDateString = this.getField("FromDate1").valueAsString;

    if (startDateString=="") {

        app.alert("You must first enter the From Date.");

        event.rc = false;

    } else {

        var startDate = util.scand("mm/dd/yyyy", startDateString);

        var endDate = util.scand("mm/dd/yyyy", event.value);

        if (endDate.getTime()<startDate.getTime()) {

            app.alert("From Date must come before To Date.");

            event.rc = false;

        }

    }

}

1 reply

Inspiring
April 1, 2017

I had an incorrect statement on that form, so here is an updated link to the new form.

Dropbox - SampleForm.pdf

Thank you.

Inspiring
April 1, 2017

Updated my form again (see above link).  Added a "Clear Form" button, in case you get stuck trying to get away from the fields, and it keeps trying to make you enter the 2nd date.

It seems that it is basing the calculation on the month, only, since that part of the date comes first.  I think I've heard that you have to enter the dates as yyyy/mm/dd for these things to work, but I don't think that is very practical for the users.  Still trying to read up on this, but will welcome any help.  Thanks.

try67
Community Expert
Community Expert
April 1, 2017

No, that's not true. You can use any format you want, as long as you take it into account in your code.

And since this is a validation script you should use it as such, not as an On Blur event.