Copy link to clipboard
Copied
I need to format a currency entry field, to replace different formats users may enter, with a French Canadian style like this: 1 234,56 $
So far I have patched together the below:
var x = Number(event.value).toFixed(2); event.value =
event.value = x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ").replace(/\./g, ',') + " $";
It works if users enter these formats:
123.45
123,45
1234.56
1234,56
12345.56
12345,56
But users get an 'NaN' if these formats are entered:
1,234.56
1 234.56
1 234,56
12,345.67
12 345.67
Guessing I'm missing something for spaces or commas in the thousands separator.
Copy link to clipboard
Copied
You can't allow all possible combinations. What if the user enters "123,456".
Is that "one hundred twenty three thousand, four hundred fifty six", or "one hundred twenty three, and four hundred and fifty six thousandths"?
Copy link to clipboard
Copied
Use a regular expression to recognize the correct format and divide up the parts. The reconstruct with the desired format.
Another technique is to split the entry into parts using a regular expression that works for any separator.
For example
var aParts = event.value.split(/[\s\.\,]/) ;
aParts[aParts.length-1] will alway be the decimal bit, if the value follows any of your assumed patterns.
And that is the weakness of this approach, what happens when the user does not enter a value with a decimal? It's probably better to test the format with specific regular expressions for each case.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
You can't allow all possible combinations. What if the user enters "123,456".
Is that "one hundred twenty three thousand, four hundred fifty six", or "one hundred twenty three, and four hundred and fifty six thousandths"?
Copy link to clipboard
Copied
Use this script as a Custom Format Script to get the French format with a $:
event.value = util.printf("%,2.2f $",event.value).toString().replace(/\./gim," ");
(Les français qui liront ces lignes peuvent remplacer le $ par l'€)
And use this script as a Custom Keystroke Script to prevent the user to enter a blank space:
if(this.event.change == " ") {this.event.change = "";}
Acrobate du PDF, InDesigner et Photoshoptographe
Copy link to clipboard
Copied
Thank you all for your feedback. JR, I tried using your scripts to mixed results. Thom, I’m too much of an amateur to grasp your technique. Perhaps I can't allow all possible combinations. I don't know this language and would like to learn the basics, for instance what each character in this means: (/\B(?=(\d{3})+(?!\d))/g, " ")". I've a got a deadline so I'll stick with the format options that come with Acrobat. If there's a basic/for Dummies type of site you can recommend that would be appreciated.

