5 Replies Latest reply on Sep 5, 2016 3:06 PM by lalarsen44

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

    lalarsen44

      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()))}

        • 1. Re: Get Field to Remain Blank Until User Enters Value in Previous Field Used in Calculation
          George_Johnson MVP & Adobe Community Professional

          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?

          • 2. Re: Get Field to Remain Blank Until User Enters Value in Previous Field Used in Calculation
            lalarsen44 Level 1

            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.

            • 3. Re: Get Field to Remain Blank Until User Enters Value in Previous Field Used in Calculation
              George_Johnson MVP & Adobe Community Professional

              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.

              • 4. Re: Get Field to Remain Blank Until User Enters Value in Previous Field Used in Calculation
                gkaiseril MVP & Adobe Community Professional

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

                1 person found this helpful
                • 5. Re: Get Field to Remain Blank Until User Enters Value in Previous Field Used in Calculation
                  lalarsen44 Level 1

                  Thank you! That worked! And is helpful to see it work to see what I did wrong.

                   

                  Lori