Skip to main content
stepheng54012748
Known Participant
December 1, 2017
Answered

Writing a Javascript for a percent entry on a form....

  • December 1, 2017
  • 2 replies
  • 2470 views

Hopefully, someone can help me.  I created a form in Acrobat.  I set up one of the lines to enter as a percentage.  The issue is when I enter (for example 50 thinking it would be 50% it shows as 5,000%.)  I realize i could enter .50 and have it show up correctly.  I am not familiar with Javascripting, but I believe a script can be written so what ever is entered will show up correctly.  If I enter a 5 then it would show 5% etc.  Does anyone know how to write this?

I would greatly appreciate your help!!

Sincerely,

Steve

This topic has been closed for replies.
Correct answer Thom Parker

Do not use the built in Format, select Custom on the Format tab and enter this script into the custom formatting box.

if(!/^\s*$/.test(event.value) && !isNaN(event.value)) event.value += "%";

This takes care of adding the % symbol, Now enter this code into the Keystroke script to prevent the user from entering non-numbers.

AFNumber_Keystroke(2, 0, 0, 0, "", true);

After this the display will revert to showing you entered a number format, but is will work the way you want

2 replies

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
December 1, 2017

Do not use the built in Format, select Custom on the Format tab and enter this script into the custom formatting box.

if(!/^\s*$/.test(event.value) && !isNaN(event.value)) event.value += "%";

This takes care of adding the % symbol, Now enter this code into the Keystroke script to prevent the user from entering non-numbers.

AFNumber_Keystroke(2, 0, 0, 0, "", true);

After this the display will revert to showing you entered a number format, but is will work the way you want

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
stepheng54012748
Known Participant
December 2, 2017

Excellent!!  Thank you so much.

try67
Community Expert
Community Expert
December 1, 2017

That's how Percent fields work. Their value ranges from 0 (0%) to 1 (100%). If you want it to work differently you would need to write your own keystroke, validation and format scripts.