• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Custom Currency Script

Contributor ,
Mar 15, 2021 Mar 15, 2021

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.

TOPICS
How to , JavaScript , PDF forms

Views

1.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 16, 2021 Mar 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"?

Votes

Translate

Translate
Community Expert ,
Mar 15, 2021 Mar 15, 2021

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. 

 

 

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2021 Mar 16, 2021

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2021 Mar 16, 2021

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 = "";}

 

Capture_235.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 16, 2021 Mar 16, 2021

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines