4 Replies Latest reply on Feb 19, 2012 8:10 AM by try67

    Please help with a custom javascript

    ry3lamp

      Hello,

       

      I am a military instructor, and I am trying to create a grade report card with Acrobat X Pro.  So far I have made good progress with the help of the forum community (just searching through the pages and pages of posts), but I am stuck on this issue.

       

      Like I stated,  I am creating a report card (of sorts) where our other instructor staff can input student test grades into the fields as they [students] progress through training.  The form is showing the particular test with an input field for the grade.  Lastly, the student's overal GPA is displayed in percent format.

       

      My conundrum: I am trying to code a field that all a user [instructor] will have to input is a numerical value with or without a decmil, and the field will display it as a percentage. 

       

      If I use acrobats default percentage, the number entered into the field will be multiplied by 100 (i.e. I enter 90 and acrobat displays 9000% in the field).

       

      I would just like to be able to type in 90, and 90% be displayed; or type in 88.5, and 88.5% be displayed.

       

      With the scripts I have entered, no decmils can be displayed, and the overal score is multiplying the average by 100, thus if I entered the number 90 in the first exam, the average calculates to 9000%.

       

      I am assuming the custom format script and custom keystroke script made the average calculation script not work correctly.

       

       

      Thanks in advance.  Ryan

       

       

      I have entered the following javascripts:

       

       

      1.  Document Level Script

       

      // Document-level function
      function DigOnlyKS() {

          // Get all that is currently in the field
          var val = AFMergeChange(event);

          // Reject entry if anything but digits
          event.rc = AFExactMatch(/\d*/, val);
      }

       

       

      2.  Custom Format Script

       

      // Custom Format script for text field

      if (event.value) event.value += "%";

       

       

      3.  Custom Keystroke Script

       

      // Custom Keystroke script

      DigOnlyKS();

       

       

      4.  Custom Calculation Script for an Overal Grade Average

       

      event.value = myAverageFunction(["pk1","pk2","pk3","pk4","pk5","pk6","pk7","pk8","pk9","pk10","pk11","p k12","pk13","pk14","pk15","pk16","pk17","pk18","pk19","pk20","pk21","pk22","pk23","pk24"," pk25"]);

       

      function myAverageFunction(aNames) {

          // n = number of fields that have a numerical value

          // sum = some of values for the named fields

          var n = 0;

          var sum = 0;

          var v;

          // loop through array of name strings

          for (i = 0; i < aNames.length; i++) {

            v = this.getField(aNames[i]).valueAsString;

            // check for not a null string and not a space  and not is Not a Number

            if (v != "" & v != " " & isNaN(v) == false) {

               n++; // increment count

               sum += Number(v); // add to sum

            }

          }

          return (n != 0) ? (sum / n): "";

      }

        • 1. Re: Please help with a custom javascript
          ry3lamp Level 1

          One last thing. 

           

          I input the custom format script into the overal GPA field, and that fixed the multiplying by 100 issue.

           

          I would also like to limit the number of decmils places to 2.

           

          Thanks again.

          • 2. Re: Please help with a custom javascript
            try67 MVP & Adobe Community Professional

            This RegExp will only allow a number with a decimal point and up to two decimal digits:

            /^\d+\.?\d{0,2}$/

             

            You can use it in your custom keystroke code to validate the entered value, and then use a format code to add the percentage symbol.

            • 3. Re: Please help with a custom javascript
              ry3lamp Level 1

              Try,

               

              I'm sorry for being so ignorant with javascript.

               

              I placed that code in the custom keystroke like you suggested, but it did not work.    Is there a specific way I need to input that code in the custom keystroke area?

               

              I then tried to put it in the document level code and replaced (/\d*/, val) with your code... that really screwed thing up.

               

              So now I am stuck where I was before.

               

               

              I will try and show you what I am trying to accomplish:

               

              Right now, this is what is happening

              Exam 1
              Exam 2
              Exam 3
              Final Average
              95.5%95.3%94.3%95.033333%

               

              What I would like to do is to limit each field to 2 decmil places.

               

              Each "exam field" has the following:

              Screen Shot 2012-02-19 at 9.50.39 AM.png

               

              The "final average" field has the following:

              Screen Shot 2012-02-19 at 9.52.48 AM.png

              and this is in the custom calculations area of the "final average" field:

              Screen Shot 2012-02-19 at 9.54.46 AM.png

               

               

              Finally, I have this in the Documet JavaScripts area:

              Screen Shot 2012-02-19 at 9.56.30 AM.png

               

              I have a feeling this is an easy fix, but I just cannot figure it out.

               

              Thanks,

              Ryan

              • 4. Re: Please help with a custom javascript
                try67 MVP & Adobe Community Professional

                I don't see the regeular expression I created for you in any of those scripts. Also, there's no need to use a formatting script for a field that has a calculated value. You can simply add the percentage symbol in that calculation script.

                If you wish, send me the file to try6767 at gmail dot com and I'll see what's not working well...