Skip to main content
JR Boulay
Community Expert
Community Expert
February 19, 2025
Answered

Formatting French numbers

  • February 19, 2025
  • 2 replies
  • 598 views

Hello

I have a specific request from a customer for which I can't find the solution either in this forum or in the documentation.

 

First of all, you need to know that in French, numbers are written with a comma to separate decimals, and with a space to separate thousands.
Example: 123 456,78

 

I could do without the space between the thousands, but I absolutely need the user to be able to enter a maximum of 6 digits, plus if necessary a comma and a maximum of 2 decimal places.

 

The problem with Acrobat's number format is that it allows to enter more than two digits after the decimal point, and then rounds them down to two.

It is therefore not usable in this particular case.

 

I know how to make a keystroke script that limits input to 6 digits with a space between the thousands, but I don't know how and I can't manage the comma and the limit for decimals.

 

I know how to do it in a Format or in a Validation script, but the customer wants the user to be restricted during input, so it doesn't work.
I absolutely need a keystroke script.

If this script could also handle the space between the thousands, it would be perfect, but that's not the most important thing.

 

Thank you for your help.

Correct answer Nesa Nurani

See if this works (without a thousand separator):

 

if (!event.willCommit) {
 var selStart = event.selStart;
 var selEnd = event.selEnd;
    
 var newValue = (selStart === 0 && selEnd === event.value.length) 
  ? event.change
  : event.value.slice(0, selStart) + event.change + event.value.slice(selEnd);

  event.rc = /^(\d{0,6}(,\d{0,2})?)?$/.test(newValue);}

EDIT:
If you want manually input space, try with this:
/^(\d{0,3}(?:\s?\d{0,3})?(,\d{0,2})?)?$/

 

2 replies

JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
February 19, 2025

Thank you, thank you!

It works fine.

 

I suggest you add "The JavaScript & GREP Queen" to your signature.

Acrobate du PDF, InDesigner et Photoshopographe
Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
February 19, 2025

See if this works (without a thousand separator):

 

if (!event.willCommit) {
 var selStart = event.selStart;
 var selEnd = event.selEnd;
    
 var newValue = (selStart === 0 && selEnd === event.value.length) 
  ? event.change
  : event.value.slice(0, selStart) + event.change + event.value.slice(selEnd);

  event.rc = /^(\d{0,6}(,\d{0,2})?)?$/.test(newValue);}

EDIT:
If you want manually input space, try with this:
/^(\d{0,3}(?:\s?\d{0,3})?(,\d{0,2})?)?$/