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
Copy link to clipboard
Copied
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"]);
}
}
Copy link to clipboard
Copied
You're comparing a string to a date object in this line:
if(sStart < d2 )
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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 )
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
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;
}
}

