22 Replies Latest reply on Apr 24, 2014 10:09 AM by Ruezo

    Conditional JavaScript for Tax Form Fields

    Ruezo Level 1

      I'm not sure how to compose a keyword search for this solution.  So,  please redirect me to an existing solution if it exists.  Also, I am not familiar with JavaScript, but can struggle through it.  I am converting a tax form to a fillable one which calculates fields with dollar amounts.  Below are two very good examples of what I am trying to accomplish.

       

      BOX 6 FORM FIELD

      IF BOX 4 IS GREATER THAN BOX 5F ENTER BALANCE DUE (NOT LESS THAN $1.00)

       

      BOX 7 FORM FIELD

      IF BOX 5F IS GREATER THAN BOX 4 ENTER OVERPAYMENT BELOW (NOT LESS THAN $1.00)

       

      Basically, I am comparing two dollar values in two boxes, then calculate two form fields' values on the condition being true.

       


      Thank you for any help you can provide,

       

      Ruezo

        • 2. Re: Conditional JavaScript for Tax Form Fields
          Ruezo Level 1

          Thanks, GKaiseril, for the information.  However, I don't see any JavaScript which applies to conditional methods needed for the samples I listed.  I should also add that if the condition is false, the valued of the box needs to be zero.

          • 4. Re: Conditional JavaScript for Tax Form Fields
            Ruezo Level 1

            Hi Gilad,

             

            This is much more helpful.  Thank you.  It gives me a good place to start writting the statements out based on the logic of the calculation.

             

            Is it best to not have spaces in the names of the fields?  I was wondering about that as I have been working with the newer versions of Acrobat.

            • 5. Re: Conditional JavaScript for Tax Form Fields
              Ruezo Level 1

              Gilad D,

               

              Based on the document to which you directed me and a few others, this is what I managed to construct.  I haven't had a chance to check it, but would greatly appreciate the critiquing from you and any other seasoned JavaScript writer.  I know that I have some errors in it as I am pretty green with JavaScript.  Have to attempt it if I am going to learn something, right?  Below is what decided to do with Box 6.  Thought It would be a great idea to change the field's background color to red if the condition is true.

               

              // Box 6 Tax Form Field

              // IF BOX 4 IS GREATER THAN BOX 5F ENTER BALANCE DUE (NOT LESS THAN $1.00)

              // if box 5F is greater than box 4, then subtract box 4 from box 5F, change sign and change text field background color.  if false, then leave box at 0 dollars.

              // Box 4 field name "Box 4"

              // Box 5F field name "Box 5F"

               

              color.red = ["RGB", 255, 133, 133];

              var bg = color.red;

              var Box4 = this.getField ("Box 4").value;

              var Box5F = this.getField ("Box 5F").value;

              if ("Box4" > "Box5F") {

                  event.value = math.max(1, (Box4.value - Box5F.value));

                  bg = color.red;

              } else {

                  event.value = 0;

              }

               

               

              Thanks,

              Ruezo

              • 6. Re: Conditional JavaScript for Tax Form Fields
                try67 MVP & Adobe Community Professional

                There are several problems with your code, to wit:

                1. color.red is a pre-defined constant. You should not overwrite it with your own values. Use a different variable name.

                2. When comparing variables do not place quotes around their names.

                3. What is this line supposed to?

                     bg = color.red;

                If you're trying to change the field's fill color to red you should do this:

                     event.target.fillColor = color.red;

                4. JS is case-sensitive, remember. So the correct name of the max function is not math.max(), but Math.max().

                5. You already defined Box4 and Box5F as the value of the fields, so you don't need to do that again in the max command.

                • 7. Re: Conditional JavaScript for Tax Form Fields
                  Ruezo Level 1

                  Well, I was in the process of trying to correct my text and got kicked out of the system.  Unfortunately, I can't correct my entry now.  So, I make my corrections here.

                   

                  // if box 5F is greater than box 4, then subtract box 5F from box A and change text field background color.  if false, then leave box at 0 dollars.

                  • 8. Re: Conditional JavaScript for Tax Form Fields
                    Ruezo Level 1

                    Gilad D,

                     

                    Regards to 1

                    The RGB is a lighter red.  If I shouldn't be messing with the red color, then how would I specify a lighter red color for the background?  I guess that I could set the format tab for the negative number to go to red color but I was concerned the red numbers would be difficult to see and show up as a light gray when printed in black and white.  What are your thoughts on that?

                     

                    Regards to 2

                    Yes, I see the error of my way.

                     

                    Regards to 3

                    I wasn't sure on the "bg = color.red;" and needed to know.  Found it in an example on the Internet.

                     

                    Regards to 4

                    Thanks, oversight on my part.

                     

                    Regards to 5

                    The name of the text fields "Box 4" and "Box 5F" have spaces in the name.  Wouldn't this be a problem?  Would it cause problems attempting to compare fields with spaces in the names?  Asked about the spaces in text field names earlier.  Should I avoid spaces in text field names in the future for this very reason and to keep the script lean and clean? 

                     

                    Message was edited by: Ruezo

                    • 9. Re: Conditional JavaScript for Tax Form Fields
                      try67 MVP & Adobe Community Professional

                      1. Define a variable and assign it the color value you want. You can use something like this:

                      var myRed = ["RGB", 255, 133, 133];

                       

                      5. The name of the fields doesn't matter. It can contain spaces or not. What matters is the name of the variabales, and those can't contain any spaces and should not be placed in double-quotes in your code.

                       

                      Also, I don't understand your use of the max() method. Just compare the values directly to one another, like so:

                       

                      if (Box5F>Box4) {

                       

                      } else {

                       

                      }

                      • 10. Re: Conditional JavaScript for Tax Form Fields
                        Ruezo Level 1

                        Thanks, Gilad.  This is very helpful.  Think this is the most I have learned from a single post on a language when a challenge is properly addressed.

                         

                        Regards to 1

                        I thought that you were going suggest for me to create my own color.  Makes sense now.

                         

                        Regards to 5

                        So, [if (Box 5F>Box 4)] will work the same if I don't want waste the time to define them.  Other languages spaces are a big problem and need to be avoided.

                         

                        The use of the max a part to fulfill the line question requirement in the original post question.

                        BALANCE DUE (NOT LESS THAN $1.00)

                        Is max the wrong way to go about handling this result requirement for the tax field?

                         

                        Thanks.

                        • 11. Re: Conditional JavaScript for Tax Form Fields
                          try67 MVP & Adobe Community Professional

                          So, [if (Box 5F>Box 4)] will work the same if I don't want waste the time to define them.

                          No! Notice that in my code I used Box5F and Box4 (no spaces!) which are the names of the variables you defined earlier. That definition must stay for it to work correctly. Again, the field names may contain spaces, but the variable names may not.

                           

                          Regarding the max: Maybe I misunderstood... What should happen in these cases:

                          - Box 5F is bigger than Box 4 by more than 1

                          - Box 5F is bigger than Box 4 by less than 1

                          - Box 5F is smaller than (or equal to) Box 4

                          - Box 5F and/or Box 4 are empty

                          • 12. Re: Conditional JavaScript for Tax Form Fields
                            Ruezo Level 1

                            Finally, had a chance to test the Box 7 JavaScript, since the form is filled out to generate results for that field.  Box 7 format tab in the properties is configured for number with dollar sign.

                             

                            My Acrobat XI is not liking the spaces on the field names.  Should have created them without spaces as I was originally thinking to do.  Eliminating the space issue did expose different one which would also occur with the Box 6 script, as well.  Is it better to use the block delimiters than the single line method?  I am using Acrobat XI to create the fields.

                             

                            var Box4 = this.getField ("Box 4").value;

                            var Box5F = this.getField ("Box 5F").value;

                            if (Box5F>Box4) event.value = Math.max(1,(Box5F.value - Box4.value));

                            else event.value = 0;

                             

                            It returns the error, "The value entered does not match the format of the field [box]"

                             

                            I had a gut feeling the else statement might create a format conflict.  So far, not seeing any references to correcting this,... still looking until someone posts a solution before I find it.

                            • 13. Re: Conditional JavaScript for Tax Form Fields
                              Ruezo Level 1

                              Okay, thanks for the clarification on the spaces.  Understood now.

                               

                              Whatever the result is for Box 6 and Box 7 the amount cannot be less than $1.00.  So, the result has to be either the max of 1 or the result of the calculation ($1.01 - ~ ), which ever is greater.  If the calculation falls under $1.00 the max method would select the "1".  I see this similar logic on various tax, invoice and financial forms.  The test should cover everything.  If not, the field needs to be $0.00.

                              • 14. Re: Conditional JavaScript for Tax Form Fields
                                try67 MVP & Adobe Community Professional

                                In that case using the max() function is a good idea.

                                 

                                The error is caused because you didn't solve the issue I mentioned in my original point #5.

                                • 15. Re: Conditional JavaScript for Tax Form Fields
                                  Ruezo Level 1

                                  Holy smokes, I had trouble trying to log onto the forum for several hours after getting home from work.

                                   

                                  Thanks, Gilad.  I was not seeing where you were referencing my redundant definitions.  The script is working.  Had to make changes to the RGB values to get it working.  Noticing post here and other forums using lower values.  One thing I realized during my testing is that I need to include a fillColor in the Else block to return the fill back to transparent.  What is the way to specify transparent?

                                   

                                  I did a little more research into the BALANCE DUE (NOT LESS THAN $1.00).  The calculation has to result in $1.00 or more.  Otherwise the amount needs to be zero.  So, I am going to have to do something other than the Math.max method.  Any suggestions there while I rummage through JavaScript guides and the Internet for direction?  Below is the working script with our corrections.

                                   

                                  var myRed = ["RGB", 1, 0.5, 0.5];

                                  var Box4 = this.getField ("Box 4").value;

                                  var Box5F = this.getField ("Box 5F").value;

                                  if (Box4 > Box5F) {

                                      event.value = Math.max(1, (Box4 - Box5F));

                                      event.target.fillColor = myRed;

                                  } else {

                                      event.value = 0;

                                  }

                                  • 16. Re: Conditional JavaScript for Tax Form Fields
                                    try67 MVP & Adobe Community Professional

                                    The issue was in this line:

                                    if (Box5F>Box4) event.value = Math.max(1,(Box5F.value - Box4.value));

                                    And you solved it in your new code by removing the incorrect additional reference to the value property.

                                     

                                    Transparent has a built-in color name:

                                    color.transparent

                                     

                                    I think you should do it like this, finally:

                                     

                                    var myRed = ["RGB", 1, 0.5, 0.5];

                                    var Box4 = Number(this.getField ("Box 4").value); // If the field is empty, the value returned by this is 0

                                    var Box5F = Number(this.getField ("Box 5F").value); // If the field is empty, the value returned by this is 0

                                     

                                    var diff = (Box5F-Box4);

                                    if (diff>1) {

                                         event.value = diff;

                                         event.target.fillColor = myRed;

                                    } else {

                                         event.value = 0;

                                         event.target.fillColor = color.transparent

                                    }

                                    • 17. Re: Conditional JavaScript for Tax Form Fields
                                      Ruezo Level 1

                                      Thanks, Gilad!

                                       

                                      I thought color.transparent was a predefined configuration but wasn't sure about using it.  I had the code in for the red background and didn't see any changes to the background while testing.  Then, I realized that I needed to turn off the Field Highlighting to see the background, just an extra note for everyone else out there.

                                       

                                      Yep, that is close to what I was going to do on a different conditional to handle the variation in the logic.  I was going to use something along the lines of the below method.  Eventually, it would evolve to something similar to your suggestion.

                                       

                                      if ((Box4 > Box5F) && (Box4-Box5F >= 1))

                                       

                                      Now that I am thinking about it more, I would have to use my conditional to test for the greater value distinction and whether or not the value is equal to or greater than one.  Since, this script is for Box 6 we need to test for Box 4 being greater than Box 5F.  So, I think the only difference between your conditional and mine is that mine does an additional test for a value to be greater, right?  Or, could I be missing some logic from your version?

                                      • 18. Re: Conditional JavaScript for Tax Form Fields
                                        try67 MVP & Adobe Community Professional

                                        You really need to consult the API reference if you want to write code like this:

                                        http://www.adobe.com/devnet/acrobat/javascript.html

                                         

                                        In my code I'm testing if the difference between the values is bigger than 1 (although in my calculation it seems I've reversed the order of the fields). So if the first field is not bigger than the second one, the result will be negative and the field will be reset anyway.

                                        • 19. Re: Conditional JavaScript for Tax Form Fields
                                          Ruezo Level 1

                                          Okay, I see what I overlooked with what you suggested. Yep, it will work on Box 6 and Box 7 based on the order of the fields in the subtraction.  Below is the final tested and working code.

                                           

                                          var myRed = ["RGB", 1, 0.5, 0.5];
                                          var Box4 = Number(this.getField ("Box 4").value);
                                          var Box5F = Number(this.getField ("Box 5F").value);
                                          var Diff6 = (Box4-Box5F);
                                          if (Diff6>=1) {
                                              event.value = Diff6;
                                              event.target.fillColor = myRed;
                                          } else {
                                              event.value = 0;
                                              event.target.fillColor = color.transparent
                                          }

                                           

                                          Works beautifully.  Thanks for the coaching.

                                          • 20. Re: Conditional JavaScript for Tax Form Fields
                                            Ruezo Level 1

                                            Gilad,

                                             

                                            I was working with the colors and see that the higher RGB numbers used in Windows OSs do not work the same in JavaScript here.  I found a few forum references on colors but nothing that explains the difference and how to convert it.  I had to do a bit of trial and error with my knowledge of graphics to get the color I desired.  Do you know of any good references on this subject matter?

                                             

                                            Hey, I am liking this JavaScript.  Only worked with it a bit over a week now.

                                             

                                            Thanks again for the help.

                                            • 21. Re: Conditional JavaScript for Tax Form Fields
                                              try67 MVP & Adobe Community Professional

                                              Keep in mind that the values you use have to be between 0 and 1... So if you're converting from a regular RGB color (0-255), you need to divide those values by 255 first.

                                              • 22. Re: Conditional JavaScript for Tax Form Fields
                                                Ruezo Level 1

                                                Just what I thought.  That is close to the method I used to get a close color.  Then, adjusted the number by a tenth one way or another.  It seems that one decimal place is acceptable.

                                                 

                                                Thanks for that suggestion.  I will add that to my notes.