5 Replies Latest reply on Oct 15, 2010 9:19 AM by Josh68

    Help with calculated field displaying/notdisplaying zero

    neodiem

      Hi all,

       

      I am working on a few forms that have a fields that get totaled and the total is displayed.

       

      The problem I am running into is that is I use the standard built-in calculation script the 0 is always displayed.

       

      I tried to use a custom calculation field, using.

       

      var v1 = +getField("ss_1").value;

      var v2 = +getField("ss_2").value;

      var v3 = +getField("ss_3").value;

      var v4 = +getField("ss_4").value;

      var v5 = +getField("ss_5").value;

       

      var sum = v1 + v2 + v3 + v4 +v5;

       

      if (sum!==0) {

      event.value = sum;

      } else {

      event.value = "";

      }

       

      The problem I ran into is that if all variables where legitimately Zeros the calculated field would still not display the zero.

       

      Finally I wanted to try putting the code in to the v1 field as a on blur action to make the calulated field visible, but I am not sure what to use as the value.

       

      I am not a programmer, just a designer who is trying to learn.  any help is much appreciated.

       

      Thanks

        • 1. Re: Help with calculated field displaying/notdisplaying zero
          try67 MVP & Adobe Community Professional

          Use != instead of !==

          • 2. Re: Help with calculated field displaying/not displaying zero
            gkaiseril MVP & Adobe Community Professional

            It appears you want the 0 total to be suppressed only when all the fields to be summed are empty strings.

             

            JavaScirpt treats empty numeric values and numeric zero values as equal, but an empty character string and a zero string are not equal. Knowing this one can then develop a script to determine if a series of values are all empty or any one of the values is not empty even if it contains a zero value. One can obtain the string value of field by using the 'valueAsString" property. With this knowledge, one can test for non-empty strings. Since this code will be repeated several times, one could create a function that reuses the repeated code. By adding a logical variable to track if all the fields are empty strings and then setting the return value to an empty string when all the input fields are empty strings one could use:

             

            function aSum(aFieldNames) {
            /*
            sum an array of field names return sum if any field is non-blank
            return empty string if all fields are empty strings
            */

            var sum = 0;
            var bSupress = true;
            var sItem;
            // loop through the fields
            for (i = 0; i < aFieldNames.length; i++) {
            sItem = this.getField(aFieldNames[i]).valueAsString;
            if (sItem != '') bSupress = false; // non-empty string so force display
            sum += Number(sItem); // sum values
            } // end field loop

            if(bSupress) sum = ''; // all fields are empty strings

            return sum;}

            // end of function

             

            /* custom calculation script to sum fields and suppress result
            if all fields are null strings */
            event.value = aSum(["ss_1", "ss_2", "ss_3", "ss_4","ss_5"]);

            • 3. Re: Help with calculated field displaying/not displaying zero
              Josh68

              Thanks, GKaiseril. I took your example and modified (with my own field names) it in a way that I think more closely addresses the original question, since I was trying to achieve the same thing. I was trying to supress the final total if my row totals all equaled zero, not if they were empty strings. I added in parens on the if statements - not sure if that was necessary - and then also took sum, appended a dollar sign, and forced it into a numeric dollar value using .toFixed(2) (with two decimal places). Hope this is helpful, and please tell me if my code could be improved (I'm sure it could).

               

              function aSum(aFieldNames) {
              /*
              sum an array of field names, return sum if any field is non-blank,
              return empty string if all fields are empty strings
              */

               

              var sum = 0;
              var bSupress = true;
              var sItem;

               

              // loop through the fields
                  for (i = 0; i < aFieldNames.length; i++) {
                      sItem = this.getField(aFieldNames[i]).value;
                          if (sItem != 0) { // if the value isn't zero
                              bSupress = false; // not zero, so force display
                          }
                      sum += Number(sItem); // sum values
                      dolVal = '$' + sum.toFixed(2); // add dollar sign and force 2 decimal places
                  } // end field loop

               

                  if(bSupress) {
                      dolVal = ''; // all fields have a value of zero, so dolVal is set to an empty string (no display)
                  }

               

              return dolVal;
              }

               

              // end of function

               

              /* custom calculation script to sum fields and suppress result
              if all fields are null strings */

               

              event.value = aSum(["Price300_ea","Price840_ea","Price350_ea","Price280_ea","Price500_ea","Price400_ea" ,"Price375_ea","Price375_ea_2","Price375_ea_3"]);

              • 4. Re: Help with calculated field displaying/not displaying zero
                canucksgirl Level 1

                hey Josh68 - Thanks for the input

                 

                Turns out the following code works great;

                 

                On the "total" boxes (each row has one & for subtotal etc) I have the validation script set to:

                 

                     // null filed value if entry is zero

                     if(event.value == 0) event.value = "";

                 

                Under Calculate>Simplified Field Notation:

                 

                     (QtyB1 + QtyC1)*69.95

                 

                In the code above, there are two options for the same product (style "B" & style "C", both are the same price)... so if someone inputs a qty for style "B" and/or style "C" the qty multiplies by price of $69.95 each.

                 

                As said, the issue was for rows where no qty was selected, the total box displayed $0.00 for the total... not very good for customers who print and fill out the form manually and fax it in.

                 

                The above calculation and validation script has removed the $0.00 total when no qty is selected, but still displays a total when a qty is selected.

                 

                I hope it makes sense... I'm not an expert in this by any means, but it provided the least amount of code to produce results i needed; especially when there are many "total" fields that required the validation script.

                 

                Maybe this will help someone else in the future

                • 5. Re: Help with calculated field displaying/notdisplaying zero
                  Josh68 Level 1

                  Wow, why didn't I think of that (because I used someone else's script, I guess). That's a much simpler solution.

                   

                  But I guess either way works.

                   

                  [Edit] - No, I'm wrong. The problem with my solution is that $0.00 shows up in each row total. If I had been thinking (!), the point of a workaround was to allow the form to be filled in the reader or printed for filling, which means that all totals have to default to blank. So I think canucksgirl's solution is the way to go.

                   

                  Thanks,

                   

                  Josh