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

Change Field Format Based on User Input

Explorer ,
Jan 18, 2019 Jan 18, 2019

Copy link to clipboard

Copied

Apologies if this has been asked before... I searched and couldn't find an answer.

I have several fields that need to be dynamically formatted based on a user's input. Each field should allow a user to enter text (most always will be "N/A") OR be formatted as a number with a currency symbol.

I've been playing around with the following code but am not sure if I'm approaching this correctly, and can't figure out where I should place it (Keystroke, Format, Validation, all three)?

function formatSwitch() {

if (typeof event.value =="number") {

AFNumber_Keystroke(2, 0, 3, 0, "$ ", true);

}

Thank you!

TOPICS
Acrobat SDK and JavaScript

Views

628

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 , Jan 20, 2019 Jan 20, 2019

Hi,

Rather than assuming the string being passed in is a number (or could be a number) you could force it to be a number by using parseInt, then you could work with the result.

// checking we have a number before we start.

if ( !isNaN ( parseInt ( event.value, 10)))

{

     // do your formatiing;

}

if it is "n/a" or any string that cannot be formatted into a number, then the pasreInt with return NaN and you can leave it be.

Hope this helps

Malcolm

Votes

Translate

Translate
LEGEND ,
Jan 19, 2019 Jan 19, 2019

Copy link to clipboard

Copied

Do you get any errors when entering the script or running the script?

I see you have defined a function but not included a return statement.

In what action are calling this function?

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
Explorer ,
Jan 19, 2019 Jan 19, 2019

Copy link to clipboard

Copied

Apologies - I should have clarified - I called that function from a Validation script in a test field but it didn't work and I didn't get any error messages.

I've tried a few variations including the setAction() method to change the Format/Keystroke scripts... it did set the scripts but they didn't do anything to the value in the field.

The closest I've gotten is this code placed in a Validation script.

if (typeof event.value =="number" || event.value == 0) {

AFNumber_Format(2,0,3,0,"$ ",true);

AFNumber_Keystroke(2, 0, 3, 0, "$ ", true);

}

This sometimes work, but it doesn't take effect until I change the value of the field again. (i.e. I type in a number and it is not formatted, change it to another number, and it is formatted).

I've experimented with changing "event.value" to "event.target.value" - admittedly, I don't quite understand the difference, but I added the following lines to the validation script to test what values were passed to each:

console.println(event.value + " is a " + typeof event.value + "(event.value)");

console.println(event.target.value + " is a " + typeof event.target.value + "(event.target.value)");

From an empty field, typing in "123" then changing it "321" returns the following 4 lines (and "321" is formatted as "$321.00", correctly).

123 is a string(event.value)

is a string(event.target.value)

321 is a string(event.value)

123 is a number(event.target.value)

I'm assuming my problem is because it's seeing the value as a string, but why is event.target.value a number?

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
LEGEND ,
Jan 20, 2019 Jan 20, 2019

Copy link to clipboard

Copied

LATEST

For a field there are many places for an action. For this problem I would look at the custom keystroke, custom format, and validation scripts. For displaying the result I would use the custom format script since this would display the value of the field with a form at applied. This would leave the actual value of the field as entered by the user. I could validate the entry as it is being made by the custom keystroke script or I could validate the entire entry by the custom validation script.

I would use a custom format script of:

function formatSwitch(cValue) {

var cSting= cValue

if (isNaN(Number(event.value)) == false) {

event.value = util.printf("$ %,001.3f", cValue);

}

return true;

}

formatSwitch(event.target.value)

I would then use a custom validation script of:

var cString = event.target.value.toUpperCase();

if(cString != "N/A" && isNaN(Number(cStrig)) == true) {

event.rc = false;

app.alert("Enter N/A or a number only!", 1,0);

}

You may want to consider the valid number range since values like INFINITY, -INFINITY, and scientific notation values are all valid numbers.

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 ,
Jan 20, 2019 Jan 20, 2019

Copy link to clipboard

Copied

Hi,

Rather than assuming the string being passed in is a number (or could be a number) you could force it to be a number by using parseInt, then you could work with the result.

// checking we have a number before we start.

if ( !isNaN ( parseInt ( event.value, 10)))

{

     // do your formatiing;

}

if it is "n/a" or any string that cannot be formatted into a number, then the pasreInt with return NaN and you can leave it be.

Hope this helps

Malcolm

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
Explorer ,
Jan 20, 2019 Jan 20, 2019

Copy link to clipboard

Copied

This worked perfectly! Thank you SO much!

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