5 Replies Latest reply on Mar 7, 2017 8:39 AM by Karl Heinz Kremer

    Javascript Calculation

    johnt123123

      Hi All,

       

      Using Adobe Acrobat X Pro

      I don't have any programming or Javascript experience so any help will be greatly appreciated.

       

      I am trying to figure out how to do a If/Else calculation in a Adobe form.

       

      In the field box named (S4_SU_PS) if I enter any value in that field I want it to calculate the difference.  For example I want it to ignore any value up to >£5.75. So if I was to enter £17 in that box then I would like it to return the difference of £11.25 in that same field.  If a value of lets say a lower amount is entered £4.35 then I would like that to be disregarded. So any thing below <5.75 shows up as £0.00.Otherwise when I come to adding up the value in this filed it will be incorrect as I am only interested in adding anything above the £5.75 if that makes sense.

       

      Sorry if haven't been clear in explaining in what I am trying to achieve.

       

      Many thanks in advance.

       

      Regards

      John

        • 1. Re: Javascript Calculation
          Karl Heinz Kremer Adobe Community Professional

          The following script should do what you want to do:

           

          var v = this.getField("S4_SU_PS").value;
          
          if (v <= 5.75) {
              event.value = 0;
          }
          else {
              event.value = v - 5.75;
          }
          

           

          Use this as the custom calculation script for your second field. Keep in mind that all formatting (e.g. two decimals, currency symbol, ...) are handles via the formatting options.

           

          You may want to review the syntax rules for JavaScript. The basic language is not too complex, and that will help you to understand how e.g. an if/else construct works. See here for example for a simple way to get into JavaScript programming for Acrobat: Learning to Program JavaScript for Adobe Acrobat - KHKonsulting LLC

          • 2. Re: Javascript Calculation
            johnt123123 Level 1

            Hi Karl,

             

            Thank you so much, this is absolutely Brilliant!

            The script you have provided me with does exactly what I wanted it to do.

             

            The only problem I have got now is if I enter any figure/number in the field S4_SU_PS my Total_Sum field which adds up the values in each field does not add the output calculation value. For some reason it remembers the input value that gets entered in the S4_SU_PS field and adds that up instead.

             

            Example

             

            I enter £10 in the S4_SU_PS field, it does the correct calculation and returns the value in that field as £4.30 (10 - 5.70).

            My Total_Sum field which adds all the values in each fields does not add the £4.30 rather it will add the inputted £10 value. I can't figure out why it is doing this.

             

            Once again your help will be greatly appreciated.

            Many thanks

             

            Regards

            John

            • 3. Re: Javascript Calculation
              Karl Heinz Kremer Adobe Community Professional

              This is usually an indication of a problem with your calculation order: The Total_Sum field is calculated before the field value depending on S4_SU_PS gets calculated. You can change the calculation order in Acrobat X by selecting the following while in the form editor:

               

              2017-03-06_12-47-30.png

               

              Other Tasks>Edit Fields>Set Field Calculation Order...

              • 4. Re: Javascript Calculation
                johnt123123 Level 1

                Hi Karl,

                 

                Thank you so much I have now set the field calculation order and this has resolved that problem but now I seem to have another problem .

                 

                The JavaScript that I have used for this calculation seems to interfere with other fields on the same page. Here is a snapshot of where I have incorporated the script.

                 

                 

                Every time I put a value in another field it resets the returned value from the script calculation in S4_SU_PS back to zero again e.g 0.00.

                In some instances it will start to deduct the return value in the S4_SU_PS from the number that has been entered in a different field. I know this sounds really bizarre. I can't figure out why it is doing this.

                 

                Once again your help will be greatly appreciated.

                Many thanks

                 

                Regards

                John

                • 5. Re: Javascript Calculation
                  Karl Heinz Kremer Adobe Community Professional

                  Unfortunately, I cannot see what's actually going on in the script, because this "preview" does not show the whole script. You can copy and paste the complete script into a reply here.

                   

                  But, even without that, I am suspecting that I know what's going on: Are you trying to modify the field that the user has actually modified? Or, in different words: Is this the calculation script for the field S4_SU_PS - the same field that the user entered data in? If that's the case, then that's your problem - you cannot have a field that the user enters data in and then try to calculate something based on that and write it back to the field. If that's not the case, then we may need to see more than just the script in this field. Would you be able to share your file? If so, take a look here for information about how to share a link to your file: Share Documents via Adobe's Document Cloud - KHKonsulting LLC