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

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

New Here ,
Mar 12, 2019 Mar 12, 2019

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"]);

}

}

TOPICS
PDF forms
882
Translate
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 ,
Mar 13, 2019 Mar 13, 2019

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

if(sStart < d2 )

Translate
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
New Here ,
Mar 13, 2019 Mar 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?

Translate
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 ,
Mar 13, 2019 Mar 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 )

Translate
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 ,
Mar 13, 2019 Mar 13, 2019

PS. You're not doing this correctly.

Move the entire code to the field's custom validation script and replace these two lines:

event.value == "";

resetForm(["EffectiveDate"]);

With this:

event.rc = false;

Translate
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
New Here ,
Mar 14, 2019 Mar 14, 2019
LATEST

Thank you both to try67 and gkaiseril for your quick responses! I was able to use what you've provided to finally make this work for my final text field "Requested effective date".

gkaiseril thank you for the added comments to the code to make it clear their purposes.

event.value==""; included or not, the field appear to work the same way correctly.

I ended up putting this modified code under the Calculate\Custom calculation script option:

var sStart = this.getField("Requested effective date").value;
// get requested effective date field value;

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

d2.setHours(0,0,0,0); // set system date 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 system date value;

app.alert("Effective Date entered is earlier than today's date.");

event.value=="";

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

}

}

Translate
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
LEGEND ,
Mar 13, 2019 Mar 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;

}

}

Translate
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