Is it possible to automatically add a comma after every 6 characters has been typed in a text field?

I want Acrobat to automatically add a comma after every 6 characters has been typed in by the user.


Mikey B

2 Answers

This may be possible you would get the best help by posting your query in the Acrobat scripting forum on the adobe forums:

Michael Kazlow   

It would be possible to do this when typing in. However, what happens when the user edits the string?

Therefore, we have a few other places where to accomplish this formatting. If further editing should be possible, and the commas are strictly for viewing, we would chose the Format event. If, however, we need to continue working with the formatted string, and we are willing to accept a bit more complicated editing, we can do it in the Validate event.

Let's look at the Format event script. There is a very suitable function, util.printx() which we can use. The only issue we may have is that we do not know the length of the string to format in advance, which also means that we can not use a fixed formatting string. We have to dynamically assemble our formatting string. We would do this like this:

var instr = event.value.toString() ;

var nblk = Math.max(0,Math.floor((instr.length) / 6)) ;

var remi = instr.length % 6 ;

var fostr = "" ;

for (var foc = 0 ; foc < nblk ; foc++) {

if (remi == 0 && foc == nblk-1) {

fostr += "??????" ;

} else {

fostr += "??????," ;



for (var rec = 0 ; rec < remi ; rec++) {

fostr += "?" ;


event.value = util.printx(fostr, instr) ;

and that would give the desired result.

Note that the reason for this code being rather complicated is that we are dynamically building our format string, depending on the length of the entered string. If we have a known maximum length of the entered string, we could use a static format string. However, we still may have to make sure that there is no trailing comma when the string's length is a multiple of 6.

Max Wyss   

Please specify a reason: