Skip to main content
Participant
November 28, 2020
Question

Apply format using JS

  • November 28, 2020
  • 1 reply
  • 922 views

I stumbled upon Karl Heinz Kremer's article called: "Apply Standard PDF Form Field Formatting/Keystroke/Validation Events to Fields via JavaScript".

A button runs this fine:

var f = this.getField("Text2");
f.setAction("Format", "AFNumber_Format(2, 0, 0, 0, \"\", true);");
f.setAction("Keystroke", "AFNumber_Keystroke(2, 0, 0, 0, \"\", true);");

But if the fields contained data before the script is ran and if I manually enter the same value, it does not apply the new format.

Can you help?

Martin

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
November 29, 2020

You have to change the data to something else than what it originally was in order for the script to "kick in".

You can even do that in your original code, by adding these lines to the end of it:

var oldValue = f.value;

f.value = "";

f.value = oldValue;

M5D66Author
Participant
December 1, 2020

TRY67,

 

Thansk you for your reply.  After I posted the question, I thought of this (almost identical to your suggestion):

 

var f = this.getField("Text2");
var oldValue = f.value
var myDecimals = app.response("decimals?")

f.setAction("Format", "AFNumber_Format(myDecimals, 0, 0, 0, \"\", true);");


f.setAction("Keystroke", "AFNumber_Keystroke(myDecimals, 0, 0, 0, \"\", true);");

f.value= oldValue

 

Martin