Javascript setAction "Format" method fails if Field hasn't been manually set to "Custom"

Adobe Community Professional ,
May 17, 2022 May 17, 2022

Copy link to clipboard

Copied

Hi all, another newbie question. I'm trying to set the formatting of many "Number" fields to "Custom" with a custom javascript. Basically what is posted by Max here. However, this method only works for me if the fields are already set to "Custom". Is there a way to set the formatting to "Custom" in the javascript?

Here is my test code (note that this code works as expected only if the field already has a custom formatter):

var myFields = ["difference.1", "difference.2", "difference.3"];
for (var i = 0; i < myFields.length; i++) {
    this.getField(myFields[i]).setAction("Format", "event.value = util.printf(\"$%,0.0f\",event.value);");
}

Any help much appreciated.

- Mark

TOPICS
JavaScript , PDF forms

Views

57

Likes

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

Adobe Community Professional , May 17, 2022 May 17, 2022

No, but it's not needed. I tried your code and saw the same result as you described, but I know what's causing it.

You see, the Number format is not just a Format event. It's also a Keystroke event (to prevent the user from entering letters into the field, for example). So unless you overwrite that, too, the hidden code used when you selected Number remains in tact, and the application still thinks the field is set to Number.

So add this line to your code to overcome that:

 

this.getField(myFiel

...

Likes

Translate

Translate
Adobe Community Professional ,
May 17, 2022 May 17, 2022

Copy link to clipboard

Copied

No, but it's not needed. I tried your code and saw the same result as you described, but I know what's causing it.

You see, the Number format is not just a Format event. It's also a Keystroke event (to prevent the user from entering letters into the field, for example). So unless you overwrite that, too, the hidden code used when you selected Number remains in tact, and the application still thinks the field is set to Number.

So add this line to your code to overcome that:

 

this.getField(myFields[i]).setAction("Keystroke", "");

Likes

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
Adobe Community Professional ,
May 18, 2022 May 18, 2022

Copy link to clipboard

Copied

LATEST

Just the sort of info I was hoping for! Thanks very much.

- Mark

Likes

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