2 Replies Latest reply on Mar 21, 2015 7:10 AM by gkaiseril

    Conditional adding and substracting numbers with Acrobat Javascript.

    2009flash Level 1

      Hello,

       

       

      I'm having the following Javascript problem, and i'm hoping that someone on this forum can help me solve it.

       

      I have 2 checkboxes, the field names are the same (MinusAlreadyPayed), but they

      have different export values.

      The export value of the first checkbox is "No", the export value of the other one is "Yes".

       

      Next to the checkboxes is a field called "AlreadyPayed" where an amount (euro currency) can be entered.

       

      Further down the form i placed the number (currency) field "Cost1" and below that another number (currency) field called "Cost2".

      Below that field there is a copy of the "AlreadyPayed" field, i placed an transparent button above it, so it can't be filled in.

      Below that there is another number (currency) field called "Total".

       

      This field should add the fields "Cost1" and "Cost2" and subtract the field "AlreadyPayed", but only if the exportvalue

      of the field "MinusAlreadyPayed" is "Yes" (second checkbox selected).

       

      Is there a way to do this ?

       

      Thanks   :-)

       

      Flash.

        • 1. Re: Conditional adding and substracting numbers with Acrobat Javascript.
          try67 MVP & Adobe Community Professional

          Sure. You can use something like this as the total field's custom calculation script:

           

          var cost1 = Number(this.getField("Cost1").value);
          var cost2 = Number(this.getField("Cost2").value);
          var alreadyPaidAmount = this.getField("MinusAlreadyPayed").value=="Yes" ? Number(this.getField("AlreadyPayed").value) : 0;
          event.value = (cost1 + cost2) - alreadyPaidAmount;
          

           

          On a side-not, placing one field on top of another is not a good idea, as the z-order of the fields will change each time you edit one of them. I would look for another solution, like renaming the check-box and using a script to populate it with the value of "AlreadyPayed".

          • 2. Re: Conditional adding and substracting numbers with Acrobat Javascript.
            gkaiseril MVP & Adobe Community Professional

            Another approach could be:

             

            var cost1 = his.getField("Cost1").value 

            var cost2 = Number(this.getField("Cost2").value); 

            var alreadyPaidAmount = 0;

            event.value = (tNumber(his.getField("Cost1").valuecost1)  +Number(his.getField("Cost2").value) - (this.getField("MinusAlreadyPayed").value=="Yes") * this.getField("AlreadyPayed").value);

             

            It works by using the fact that a logical true has a value 1 and the logical false has a value of 0. So if I can multiply by a value by a logical true I have the number but if I multiply by the logical false or zero get a zero value and then I multiply by that value to get the value or a zero.