• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to validate start dates and end dates

Explorer ,
Jul 25, 2019 Jul 25, 2019

Copy link to clipboard

Copied

I have a form related to contracts. There are two fields for the contract terms: start date and end date.

My wish for the validation of these two fields:

1) That users are alerted to enter a start date before they enter an end date

2) That users are alerted to correct the end date if it comes before the start date

Here was my attempt:

event.rc = true;

if (event.value) {

    var startDateString = this.getField("StartDate_af_date").value;

    if (startDateString=="") {

        app.alert("You must first fill-in the contract start date.");

        event.rc = false;

    } else {

        var startDate = this.getField("StartDate_af_date").value;

        var endDate = event.value;

        if (endDate>startDate) {

            app.alert("End date of the contract must be AFTER start date. Please correct.");

            event.rc = false;

        }   

    }

}

No matter what end date is entered, the alert pops up.

I'm a self-taught scripter and cobbled this together from another forum answer. I know I am missing something critical here but my research hasn't helped me.

Thank you!

TOPICS
Acrobat SDK and JavaScript

Views

428

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 25, 2019 Jul 25, 2019

Copy link to clipboard

Copied

LATEST

You can't compare date strings like that. It's more complicated than that.

You need to convert them from strings to Date objects (using the scand method of the util object) and then you'll be able to compare them using the getTime method.

See these tutorials:

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-2

https://acrobatusers.com/tutorials/working-date-and-time-acrobat-javascript-part-3-3

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines