Skip to main content
Inspiring
March 15, 2021
Answered

Custom Currency Script

  • March 15, 2021
  • 4 replies
  • 2550 views

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.

This topic has been closed for replies.
Correct answer try67

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"?

4 replies

Inspiring
March 16, 2021

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.

JR Boulay
Community Expert
Community Expert
March 16, 2021

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 Photoshopographe
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
March 16, 2021

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"?

Thom Parker
Community Expert
Community Expert
March 16, 2021

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. 

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often