8 Replies Latest reply on Sep 8, 2014 4:24 PM by gkaiseril

    Display percent sign in form field

    blueskies_7

      Hello,

       

      I was hoping someone could help with 2 small scripting questions:

       

      1)  I'd like to have a form field include a percent sign after someone inputs a number.  I tried using the Percentage value under Format, but it automatically multiples by 100, which I don't want to do.  If someone inputs "20", I'd just like it to show up as "20%."  

       

      2)  I have one form field where I'd like a the tooltip name to display in the field, so it's clear what they have to input without them having to mouseover -- I'd like it to say "Name here," but then disapper when they enter copy.

       

      I am a total newbie (if that wasn't obvious!) and really appreciate any help you can offer.

       

      Thank you!
      Amy

        • 1. Re: Display percent sign in form field
          George_Johnson MVP & Adobe Community Professional

          For #1, you'd normally use a Format script. What format do you currently have the field set to?

           

          For #2, you'd also use a custom Format script, something like:

           

          // Custom Format script for text field
          (function () {
          
              // Display tooltip in field if it's blank
              if (!event.value) {
                  event.value = event.target.userName;
              }
          
          })();
          
          1 person found this helpful
          • 2. Re: Display percent sign in form field
            blueskies_7 Level 1

            Thanks for your quick reply, George.  I currently have the format set to "None."  But I can change that to whatever you recommend.  I would just like a % sign to automatically come up after a number  -- so someone could enter "20" and it would show up in the field as "20%"    The standard Percentage format multiples by 100, which I don't need.

            • 3. Re: Display percent sign in form field
              George_Johnson MVP & Adobe Community Professional

              In that case, the custom Format script would be:

               

              // Custom Format script for text field
              if (event.value) event.value += "%";
              

               

               

              But you'd normally also want to include a custom Keystroke script so that the user can only enter digits. If want to do that as well, post again and indicate if you want to allow a decimal as well.

              1 person found this helpful
              • 4. Re: Display percent sign in form field
                blueskies_7 Level 1

                A custom keynote script so they can only enter numbers would be great!   I don't need any decimal points.  It would only be whole numbers.  Thank you.

                • 5. Re: Display percent sign in form field
                  George_Johnson MVP & Adobe Community Professional

                  For the custom Keystroke script, you need to add a document-level JavaScript that includes the following function:

                   

                  // 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);
                  }
                  

                   

                  To add a new document-level JavaScript in Acrobat 9, select: Advanced > Document Processing > Document JavaScript...

                   

                  and enter a new (e.g., Keystrokes), click add, delete the empty function it provides by default, and enter the code shown above.

                   

                  You would then call this function like this as the field's custom Keystroke script:

                   

                  // Custom Keystroke script
                  DigOnlyKS();
                  

                   

                  Do this for any other field you want to behave this way. Include the custom Format script shown earlier as well.

                  • 6. Re: Display percent sign in form field
                    blueskies_7 Level 1

                    PERFECT!   Works beautifully.  Thank you so much for your help!

                    -Amy

                    • 7. Re: Display percent sign in form field
                      SER

                      Hello, I added your % script to a Rate field which is a var in a loan amortization payment.  My monthly payment calc is now no loner working. Script is as follows:

                       

                       

                      //document level script;

                      function PMT(r, Nper, Pv) {

                      // compute the periodic payment amount using the following information;

                      // r - interest rate per period;

                      // Nper - total number of payments for the loan;

                      // Pv - present value of the loan;

                      var Payment = "";

                      var nDivisor = 1 - Math.pow((1 + r), - Nper);

                      if (nDivisor != 0) {

                      Payment = (r * Pv) / (1 - Math.pow((1 + r), - Nper));

                      } // end non-zero divisor;

                      if(isNaN(Payment)) Payment = "";

                      return Payment;

                      } // end PMT function;

                       

                      function GetField(cName) {

                      // return the field object for parameter cName;

                      var oField = this.getField(cName);

                      if (oField ==  null) {

                      app.alert("Error accessing field \"" + cName + "\"", 0, 0);

                      }

                      return oField;

                      } // end GetField function;

                      // end document level script;

                       

                      var AnnualRate = GetField("Rate");

                      var NumberPayments = GetField("Amort");

                      var PresentValue = GetField("Loan amount");

                      if(PresentValue == null) app.alert("error getting present value field");

                      event.value = PMT( AnnualRate.value / 12, NumberPayments.value, PresentValue.value);

                      • 8. Re: Display percent sign in form field
                        gkaiseril MVP & Adobe Community Professional

                        Since you do not want to use the built-in Percentage format and enter the decimal value, now you have to convert the text string for you percentage to the decimal value needed for the calculation. I would look at using the RegExp to split the text value from your percentage to the decimal value needed.

                         

                        You might want to reconsider your choice for the custom keystroke.