Skip to main content
Alliosaaa
Inspiring
Jan 18, 2019 8:39 PM UTC
Answered

Change Field Format Based on User Input

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!

This topic is closed to new replies. Start a new post to keep the conversation going.
BarlaeDC
BarlaeDCCorrect answer
Community Expert
Jan 20, 2019 11:36 AM UTC

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

5 replies

BarlaeDC
Community Expert
BarlaeDCCommunity ExpertCorrect answer
Community Expert
Jan 20, 2019 11:36 AM UTC

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

Translate
Alliosaaa
AlliosaaaAuthor
Inspiring
Jan 20, 2019 3:16 PM UTC

This worked perfectly! Thank you SO much!

Translate
Inspiring
Jan 19, 2019 6:21 PM UTC

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?

Translate
Alliosaaa
AlliosaaaAuthor
Inspiring
Jan 19, 2019 6:47 PM UTC

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?

Translate
Inspiring
Jan 20, 2019 4:57 PM UTC

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.

Translate