6 Replies Latest reply on Sep 3, 2012 2:09 PM by George_Johnson

    Minimum and Maximum Calculations

    kcsoftware

      Hi Everyone,

       

      Most recently I was tasked to change our formulas from Excel and make them usable in pdf format (Adobe Acrobat X in Windows Vista). Generally, these formulas are max and min functions and are based on the users input which fires an action that returns a PASS or FAIL condition.

       

      The "PASS" condition would only be returned as long as the users input was in between the max and min. If the users input was out of the min and max ceiling it would return "FAIL". The problem is that I'm not sure how to aproach this with JavaScript in the PDF form and/or if I would really need too? I'll try and explain what I'm trying to accomplish here;

       

      Currently I have 4 text fields:

       

      • Text field 1 holds the min value.
      • Text Field 2 holds the max value.
      • Text Field 3 will hold the input value.
      • Text Field 4 returns a "PASS" or "FAIL" based on the input.

       

      I'm assuming that with JavaScript, I should be able to apply the following;

       

      var m1 = getField("Text1").value; //gets the minumum value

      var m2 = getField("Text2").value; //gets the maximum value

      var m3 = getField("Text3").value; //gets the input value

       

      Here is where I'm lost as I do not know where or how to apply; (I may have this all wrong).

       

      Text4 = m4

       

      if m3 >= m1 and m3 <= m2 m4 = {"PASS"}

           ElseIf m3 < m1 and m3 > m2  m4 = {"FAIL"}

       

      Thank you!

        • 1. Re: Minimum and Maximum Calculations
          George_Johnson MVP & Adobe Community Professional

          Try this as the m4 field's custom Calculate script:

           

          (function () {

           

              // Get the field values, as numbers

              var m1 = +getField("Text1").value;

              var m2 = +getField("Text2").value;

              var m3 = +getField("Text3").value;

           

              // Set this field's value

              if (m3 < m1 || m3 > m2) {

                  event.value = "FAIL";

              } else {

                  event.value = "PASS";

              }

           

          })();

           

           

          You can replace the if/else block above with the following equivalent statement:

           

          event.value = m3 < m1 || m3 > m2 ? "FAIL" : "PASS";

          • 2. Re: Minimum and Maximum Calculations
            kcsoftware Level 1

            Hi George! Thankyou very much! It worked! They should give you thousand points!

             

            I have just a couple questions, regarding the "If" statement.

             

            (I'm not sure if I am posting this question in the right area - please let me know

            so that you get credit).

             

            1. How would I go about changing the

            color condition of the of PASS or FAIL text value?

             

            PASS = GREEN

            FAIL = RED

             

            2. If the Input box (m3/Text3) is equal to nothing, or has no input, m4/Text4 would equal "N/A".

             

            Again, thank you very much George!

            1 person found this helpful
            • 3. Re: Minimum and Maximum Calculations
              George_Johnson MVP & Adobe Community Professional

              You could change the script to the following:

               

              (function () {

               

                  // Get the field values, as numbers

                  var m1 = +getField("Text1").value;

                  var m2 = +getField("Text2").value;

                  var s3 = getField("Text3").valueAsString;

                  var m3 = +s3;

               

                  // Set to "N/A" if Text3 is blank

                  if (!s3) {

                      event.value = "N/A";

                      event.target.textColor = color.black;

                      return;

                  }

               

                  // Set this field's value

                  if (m3 < m1 || m3 > m2) {

                      event.value = "FAIL";

                      event.target.textColor = color.red;

                  } else {

                      event.value = "PASS";

                      event.target.textColor = color.green;

                  } else {

                  }

               

              })();

               

              Correct typo

              1 person found this helpful
              • 4. Re: Minimum and Maximum Calculations
                George_Johnson MVP & Adobe Community Professional

                I had to correct a typo in my last post, so if you're seeing this via email, go back via the forums to see the corrected code.

                1 person found this helpful
                • 5. Re: Minimum and Maximum Calculations
                  kcsoftware Level 1

                  Hi George! - You did again! - Very helpful! It works!

                   

                  Also, I did not get a chance to go back through the forum to find the typo as I found what it was

                  right away with the extra else statement and the two brackets, so I went ahead reposted the full working

                  code here;

                   

                   

                  (function () {

                   

                  // Get field values, as numbers

                   

                       var m1 = +getField("Text1").value;

                       var m2 = +getField("Text2").value;

                       var s3 = getField("Text3).value;

                       var m3 = +s3;

                   

                  // Set to "N/A" if Text3 is blank

                   

                       if (!s3) {

                   

                           event.value = "N/A";

                           event.target.textColor = color.black;

                           return;

                       }

                   

                  // Set this field's value

                   

                       if (m3 < m1 || m3 > m2) {

                   

                           event.value = "FAIL";

                           event.target.textColor = color.red;

                   

                       } else {

                   

                           event.value = "PASS";

                           event.target.textColor = color.green;

                   

                       }

                       })();

                   

                   

                  Again George, thank you very much for your help! Also, I did not find anywhere that I could mark

                  your most recent post as correct (I was only able to mark it as helpful).

                   

                  As I am failrly new to the forum, please feel free to point me in the right direction so that you get

                  your well deserved credit.

                   

                  Thanks again!

                  • 6. Re: Minimum and Maximum Calculations
                    George_Johnson MVP & Adobe Community Professional

                    That's not the one I meant, but good catch. Here is what that one line should be:

                     

                    var s3 = getField("Text3").valueAsString;

                     

                    not this:

                     

                    var s3 = getField("Text3").value;

                     

                     

                    No need to be concerned about points.