2 Replies Latest reply on Jul 14, 2010 8:58 AM by toby.williams247

    Calculation Script

    toby.williams247 Level 1

      I am trying to set up a validation / calculation script on a form field and I'm hoping someone could assist me or at least point me in the right direction.

       

      The general idea if what I am after is A / B = C when A is greater than 0 otherwise no value or errors are returned. I am used to using Microsoft Excel and would normally enter a calculation as


         =IF(A1<>0,A1/B1,"")

       

      If anyone could help me with this I would be very grateful

       

      Thanks in advance.

        • 1. Re: Calculation Script
          gkaiseril MVP & Adobe Community Professional

          Well, JavaScript is not Excel formulas, but is closer to VBA.

           

          You can find more about Mozilla JavaScript at JavaScript . Adobe have freely down loadable Acrobat JavaScirpt at their developer network site.

           

          Acrobat JavaScript is an object orientated language, so you need to obtain an object before you can test or set a property or use a method of that object.

           

          Assuming your field name are "a1", 'b1', and 'c1' then one could use for the 'custom calculation script' for field for 'c1':

           

          // get the 'a1' field object

          var a = this.getField('a1');

          // is the value of field 'a1' greater than zero?

          if ( a.value > 0 )

          {

          // block of code to execute when 'a1' is greater than zero

          // set this field value to a1 / b1

          event.value = a.value / this.getField('b1').value;

          // end block of code for 'a1' > 0

          } else {

          // otherwise set value to zero

          event.value = 0;

          // end else block of code

          }

           

          But you may need to adjust the code for the case of a divisor of zero.

          event.value

          • 2. Re: Calculation Script
            toby.williams247 Level 1

            Thank you for all your help I was eventually able to get the result by using the following script

             

            if( this.getField("A1").value > 0)
                    event.value = this.getField("A1").value / this.getField("B1").value
            else
            event.value = "";