Copy link to clipboard
Copied
The requirement is like, Users will enter their salary, no alpa, just numbers. After finishing type, the numbers converted to currency formatting.
E.g. 1234 --> $1,234.00
Can someone help on the script on the data transformer and also need to limit user enter any other charactors but only numbers.
Note: the Salary Field is a String field.
This is where I got so far, but it doesn't work..
var salaryScript = "";
salaryScript = "var salary = getField("Salary");";
salaryScript += "var salaryEntered = salary.value;";
salaryScript += "var currency = '$' + parseFloat(salaryEntered).toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');";
salaryScript += "salary.value = currency;";
var salaryField = getField("Salary");
salaryField.setAction("OnBlur", salaryScript );
Copy link to clipboard
Copied
This is all you need:
var salaryField = this.getField("Salary");
salaryField.setAction("Format", "event.value = util.printf(\"$%,0.2f\",event.value);");
Copy link to clipboard
Copied
Your script is way over complicated and problematic. Do not use it.
Why not use the built-in currency number formatting instead?
The alternative is to use a custom format script.
Like this:
event.value = util.printf("$%,0.2f",event.value);
You should learn a bit about how field event work in Acrobat Specifically Format events.
Here's an article on the topic:
https://www.pdfscripting.com/public/Formatting-Form-Fields.cfm
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thanks for your reply. I am a start leaner of JS. And I am working on a 3rd part design tool which support JS.
I noticed the 'event.value = util.printf("$%,0.2f",event.value);' somewhere. But I don't know how to use it or how to connect with the field 'Salary' I already defined.
In the design tool, I can define it as any type of below, I chose dobule, as a result, it shows up 2000.00 but no comma and $ symbol. So I need to do with script to format the field Salary, but I how to connet the code you posted with this Filed.
Copy link to clipboard
Copied
Use Acrobat to do it. There you will be able to simply select the Currency option under the Format tab and it will take care of the code for you. No need to re-invent the wheel...
Copy link to clipboard
Copied
No, we are allowed do any modify on the Acorbat. All the work need to be done on the 3rd part design tool, then genereate a correct PDF Form for customer to use.
Copy link to clipboard
Copied
Correct it -- No, we are NOT allowed do any modify on the Acorbat. All the work need to be done on the 3rd part design tool, then genereate a correct PDF Form for customer to use.
Copy link to clipboard
Copied
This is all you need:
var salaryField = this.getField("Salary");
salaryField.setAction("Format", "event.value = util.printf(\"$%,0.2f\",event.value);");
Copy link to clipboard
Copied
Thanks for your support! It works well.

