Skip to main content
Participant
March 12, 2019
Question

I need Custom calculation script for an alert message when a user enters a date prior to the current date but still displays what the user had entered

  • March 12, 2019
  • 2 replies
  • 963 views

What I have so far but does not work. Basically I just want an alert message to pop up when the user picks a date prior to the current date (today's date) but still allows the date to be entered. The field named as "EffectiveDate" with format date: mm/dd/yyyy. I know very little when it comes to Acrobat JavaScript. Please help.

I have placed this under the Text Field Properties/Calculate/Custom calculation script section:

var sStart = this.getField("EffectiveDate").value;

var d2 = new Date();

d2.setHours(0,0,0,0);

if(sStart.length )

{

var dateStart = util.scand("mm/dd/yyyy",sStart);

if(sStart < d2 )

{

app.alert("The Effective Date entered is before today's date.");

event.value == "";

resetForm(["EffectiveDate"]);

}

}

This topic has been closed for replies.

2 replies

Inspiring
March 13, 2019

Adding comments to your code would help you and others understand what your code does.

var sStart = this.getField("EffectiveDate").value; // getf effective date field value;

var d2 = new Date(); // get system date object;

d2.setHours(0,0,0,0); // set system dte object to midnight;

if(sStart.length )

{

// must have a non-empty start date;

var dateStart = util.scand("mm/dd/yyyy",sStart); // convert start date to Date object;

dateStart.setHours(0, 0, 0, 0); // set effective date time to midnight;

if(dateStart.getTime() < d2.getTime() )

{

// error if dateStart value less than sytem date value;

app.alert("The Effective Date entered is before today's date.\nEffective date must be or after " + util.printd("mm/dd/yyyy", d2), 1, 0, "Effective Date Error");

event.value == ""; // clear the entry value;

event.rc = false; // set event return code to false, error;

}

}

try67
Community Expert
Community Expert
March 13, 2019

You're comparing a string to a date object in this line:

if(sStart < d2 )

Phala2019Author
Participant
March 13, 2019

sStart is my "EffectiveDate" field that is already formatted as a date. What code should I use to have this field value as a date object?

try67
Community Expert
Community Expert
March 13, 2019

You already converted that string to a date object, but you're not using that object in your comparison.

Change this:

if(sStart < d2 )

To:

if(dateStart< d2 )