Skip to main content
Participant
September 5, 2016
Answered

Get Field to Remain Blank Until User Enters Value in Previous Field Used in Calculation

  • September 5, 2016
  • 3 replies
  • 2738 views

I am working on a PDF form and am stuck on one of my calculations. There are two fields "Desired Start Date", which is a date entered by the user. The "Desired End Date" is calculated to be one year from date entered by user. I have that figured out-- however, before the user enters a date, I would like that field to remain empty. Currently it fills in with 01/00/000. How can I get that field to not calculate unless a date is entered by the user? So far, I've tried this but can't get the field to remain blank:

//if field “Desired Start Date” is empty, leave “to” field blank

var myStartDate = new Date(this.getField("Desired Effective Date").value);

if (((myStartDate == 0) && (getField("myStartDate").valueAsString === "")) { event.value = "";

//if field “Desired Start Date” has a date entered, use the date to calculate one year from date entered.

}

else {this.getField("to").value = util.printd("mm/dd/yyyy", new Date(myStartDate.getFullYear()+1,myStartDate.getMonth(),myStartDate.getDate()))}

This topic has been closed for replies.
Correct answer George_Johnson

Try the following as the custom calculation script of the "to" field

// Get the value of the start date field

var sStartDate = getField("Desired Effective Date").valueAsString;

// If the start date field is not blank...

if (sStartDate) {

    // Convert date string to a date object

    oDate = util.scand("mm/dd/yyyy", sStartDate);

    // Add a year

    oDate.setFullYear(oDate.getFullYear() + 1);

    // Set this field value to calculated date

    event.value = util.printd("mm/dd/yyyy", oDate);

} else {

    // Blank this field since the input field is blank

    event.value = "";

}

You may need to change the "mm/dd/yyyy" date format I used to match the formats of the date fields.

3 replies

Participating Frequently
July 3, 2023

Hello - I am trying to auto populate the week ending date which is Sundays based on the work date that my tech inputs into the PDF Form that I have created. Sunday is our weekending date. How can I do this PDF Forms? I see several tutorials, but I am not all that skilled in code and I am having a hard time figuring it out. Please help.

 

Thom Parker
Community Expert
Community Expert
July 3, 2023

Please ask this question on a new thread.

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
July 3, 2023

I do not know how to create a new thread?

Inspiring
September 5, 2016

Have you opened the Acrobat JavaScript console to see if you have any errors?

Not getting a pop message is far from having the correct code.

Have you tried to see what the value of the "Desired Start Date is?

The field is not a numeric value since it most likely will contain  some type of separator like "/", "-" , ".", or space. It is a text field. Empty text fields have a length of 0 or a value of the null string, "". I would test for one of those properties. The only exception I could think of would be if you entered the number of milliseconds since the Epoch date, January 1, 1970 at midnight.

You can use the JavaScript console to access various variables, objects or values either through code or by direct entry into the console.

Try the following code to see more information about your code:

console.show();
console.clear();
//if field “Desired Start Date” is empty, leave “to” field blank
var myStartDate = new Date(this.getField("Desired Effective Date").value);
if(myStartDate == null) console.println("error in date value converstion");
console.println("myStartDate :" + myStartDate);
console.println("typeof myStartDate: " + (typeof myStartDate));
console.println("lenght of myStartDate: " + myStartDate.length);
console.println("field myStartDate value: " + this.getField("myStartDate").valueAsString);
console.println("typeof field myStartDate value: " + (typeof this.getField("myStartDate").valueAsString));
console.println("length of field myStartDate : " + this.getField("myStartDate").valueAsString.length);

if ( ((myStartDate == 0) && (this.getField("myStartDate").valueAsString === "") ))
{
event.value = "";
//if field “Desired Start Date” has a date entered, use the date to calculate one year from date entered.
}
else {this.getField("to").value = util.printd("mm/dd/yyyy", new Date(myStartDate.getFullYear()+1,myStartDate.getMonth(),myStartDate.getDate()))
}
// converting date string to date object;
console.println("value of Desired Effetive Date: " + this.getField("Desired Effective Date").value);
var oMyDate = util.scand("mm-dd-yyyy", this.getField("Desired Effective Date").value);
console.println("Date object Desired Effective Date: " + oMyDate);
oMyDate.setFullYear(oMyDate.getFullYear() + 1);
console.println("Desired EWffective Date + 1 year: " + oMyDate);

Inspiring
September 5, 2016

Can you clarify what fields are involved in this? You mentioned fields "Desired Start Date" and "Desired End Date", and the code you posted uses "Desired Effective Date", "myStartDate", and "to" field names. What is the name of the field that this script is attached to?

Participant
September 5, 2016

I apologize-- the fields involved are the ones listed in the code. User enters date in "Desired Start Date" field. The "to" field is the one that calculates the date one year from the date entered. I want the field "to" to remain blank unless the user has entered a date.

George_JohnsonCorrect answer
Inspiring
September 5, 2016

Try the following as the custom calculation script of the "to" field

// Get the value of the start date field

var sStartDate = getField("Desired Effective Date").valueAsString;

// If the start date field is not blank...

if (sStartDate) {

    // Convert date string to a date object

    oDate = util.scand("mm/dd/yyyy", sStartDate);

    // Add a year

    oDate.setFullYear(oDate.getFullYear() + 1);

    // Set this field value to calculated date

    event.value = util.printd("mm/dd/yyyy", oDate);

} else {

    // Blank this field since the input field is blank

    event.value = "";

}

You may need to change the "mm/dd/yyyy" date format I used to match the formats of the date fields.