5 Replies Latest reply on Aug 23, 2011 1:55 PM by mlhend9

    Having problems using Percentage Field Format

    mlhend9

      Hello,

      I have a very easy calculation based on 2 input fields.  The input fields are formated as number, currency, with 2 decimal places .

      The field names are:

      ttlkey - this amount will be entered by the user.

      ttlall - this amount will be entered by the usre.

      keypercent - this is a read only field that is formated as percentage with 1 decimal place and has the following calculation script:

       

      // Calculation script
      (function () {

      // Get field values as numbers
      var key = +getField("ttlkey").value;
      var all = +getField("ttlall").value;

      // Perform calculation
      if (key || all !== 0){
      event.value = key / all;
      } else {
      event.value = "";
      }

      })();

      This works fine, however, when the user trys to enter the data into the ttlkey or ttlall field, they receive an warning msg that states: The value entered does not match the format of the the field [keypercent]

      The user can hit ok and continue entering...but, I would like to get rid of this message.

       

      Any help is greatly appreciated

        • 1. Re: Having problems using Percentage Field Format
          try67 MVP & Adobe Community Professional

          When "ttlall" is 0, you have division by zero, which is not allowed.

          1 person found this helpful
          • 2. Re: Having problems using Percentage Field Format
            mlhend9 Level 1

            In the script, I thought by using

            if (key || all !== 0){

            event.value = key / all;

            } else {

            event.value = "";

            }

             

            I was saying do not divide if either field is zero.

             

            The field will always be blank until the user enters the data into ttlkey or ttlall.  This is when the message is popping up, when the user trys to enter an amount into either of these fields.   I have checked the calculation order is correct.

             

            Is there an error in my script?

            • 3. Re: Having problems using Percentage Field Format
              try67 MVP & Adobe Community Professional

              You used the OR operator, which means that if any of the of the conditions

              is true, the entire expression is true. So if "key" is not null, then the

              value of "all" doesn't matter. You want to use the AND operator instead

              (&&).

              1 person found this helpful
              • 4. Re: Having problems using Percentage Field Format
                gkaiseril MVP & Adobe Community Professional

                Your logical statement is wrong. You need to individually test each field for zero and then logically AND the results of those two comparisons.

                 

                // Calculation script
                (function () {

                // Get field values as numbers
                var key = Number(this.getField("ttlkey").value);
                var all = Number(this.getField("ttlall").value);

                event.value = "";

                // Perform calculation
                if ( (key != 0) & (all != 0) ) {

                event.value = key / all;

                }

                }) ();

                 

                But in reality you only really need to test for a divisor of zero since zero divided by any non-zeno number is zero. If you want to suppress a zero answer then you can add code to do that

                 

                // Calculation script
                (function () {

                // Get field values as numbers
                var key = Number(this.getField("ttlkey").value);
                var all = Number(this.getField("ttlall").value);

                event.value = '';

                if (all != 0) {

                  event.value = key / all;

                }

                if (event.value == 0) {

                   event.value = "";

                }

                }) ();

                 

                But if you do not want 0% to display, you will need to play with setting the format with some additional scripting.

                • 5. Re: Having problems using Percentage Field Format
                  mlhend9 Level 1

                  Thank you!   This worked, I used the 2nd code you provided and works like a charm.