3 Replies Latest reply on Jan 30, 2012 8:06 PM by George_Johnson

    How to return a blank in a calculated field if another field contains a number?

    Jbellitta

      Hi there,

      I have a calculated field called "AutoCalc" that is automatically summing the number of days between 2 dates for the user. However, the user also has the option of manually entering the number of days themselves in another text box called "ManEntry".

      I would like the value in "AutoCalc" to return a blank if the user puts any information in the "ManEntry" field.

      Can anyone help me with the code, I am totally useless at script.

      The code in the "AutoCalc" field is currently:

      event.value = 0;var strStart = this.getField("Start1").value;var strEnd = this.getField("End1").value;if(strStart.length & strEnd.length) {var Start1 = util.scand("dd/mm/yy",strStart);var End1 = util.scand("dd/mm/yy",strEnd);var diff = End1.getTime() - Start1.getTime();var oneDay = 24 * 60 * 60 * 1000;var days = Math.floor(diff/oneDay);event.value = days;}

       

      Thanks so much!

        • 1. Re: How to return a blank in a calculated field if another field contains a number?
          gkaiseril MVP & Adobe Community Professional

          Add another 'if' statement

           

          event.value = 0;
          if(this.getField("ManEntry").valueAsString != "") {

          var strStart = this.getField("Start1").value;
          var strEnd = this.getField("End1").value;
          if(strStart.length & strEnd.length) {
          var Start1 = util.scand("dd/mm/yy",strStart);
          var End1 = util.scand("dd/mm/yy",strEnd);
          var diff = End1.getTime() - Start1.getTime();
          var oneDay = 24 * 60 * 60 * 1000;
          event.value = Math.floor(diff/oneDay);event.value = days;
          } // end date fields have value;

          } // if ManEntry is not blank;

          • 2. Re: How to return a blank in a calculated field if another field contains a number?
            Jbellitta Level 1

            Thank you so much for your quick reply!

            I just tried to add that last piece to the script but it is not working. I also tried changing the text to say "if ManEntry is blank" because I want numbers in the "AutoCalc" field only if there are NO numbers in the ManEntry field. But when i put a value in the ManEntry it is not clearing the number from the "AutoCalc" field...

            Any thoughts?

            Thanks again.

            • 3. Re: How to return a blank in a calculated field if another field contains a number?
              George_Johnson MVP & Adobe Community Professional

              Try something like the following:

               

              // Custom Calculate script for text field
              (function () {
              
                  // Blank this field if ManEntry has a value
                  if (getField("ManEntry").valueAsString) {
                      event.value = "";
                      return;
                  }
              
                  // Default value
                  var val = 0;
              
                  // Get the start and end dates
                  var strStart = getField("Start1").valueAsString;
                  var strEnd = getField("End1").valueAsString;
              
                  // Calculate the difference in days if both fields have a value
                  if (strStart && strEnd) {
                      var Start1 = util.scand("dd/mm/yy",strStart);
                      var End1 = util.scand("dd/mm/yy",strEnd);
                      var diff = End1.getTime() - Start1.getTime();
                      val = Math.floor(diff/864E5);
                 }
              
                  // Set this field value
                  event.value = val;
              
              })();
              
              

               

               

              Edit: corrected typo