Expand my Community achievements bar.

SOLVED

Copy values from a (text)string of data to two textfields

Avatar

Level 2

i have 3 fields:

- 1 dropdown, that contains several rows of data, like this "100001 | Product 1 | 23,4"

- 1 textfield, that should take only the value "100001" from the selected string in the dropdown-menu

- 1 textfield, that should take only the value "23,4" from the selected string in the dropdown-menu

The user selects the row/string from the dropdown-menu and then the other values should be automatically selected depending on the string.

Is this possible and how it could be done?

Help much appreciated : )

1 Accepted Solution

Avatar

Correct answer by
Level 6

If the number is a decimal, try changing the field to a decimal type rather than a numeric type (although it should probably work anyway).  Also, check the display pattern to make sure it's set to display the decimal digits.

What's your locale set to?

View solution in original post

10 Replies

Avatar

Level 6

There are lots of ways to do this, based on whether you want to use FormCalc or javascript scripting and which functions you want to use.  I've attached a javascript example. There is a dropdown list on the form that's initialized to a few values similar to what you included in your question.  The script is in the change event of the dropdown and looks like this:

var parts = xfa.event.newText.split('|');

Part1.rawValue

= parts[0].replace(/^\s+|\s+$/g, "");

Part3.rawValue

= parts[2].replace(/^\s+|\s+$/g, "");

The first line takes the full string that selected in the dropdown and splits it into an array of three strings using the javascript string.split() function.  It will split the string into substrings based on the '|' delimiter.  The other two lines store the values of the first and third substring into the two text fields.   The .replace(/^\s+|\s+$/g, "") stuff trims the spaces from the left and right of each substring, since your example had spaces around the '|' that get included in the substrings.

Avatar

Level 2

Thank you, i will try this tomorrow. I will add the stars after this : )

Avatar

Level 2

Can't get the code working (the sample works ok) when i try to apply it to my form.

Here's the codes:    Part1.rawValue = parts[0].replace(/^\s+|\s+$/g, "");

                             Part3.rawValue = parts[2].replace(/^\s+|\s+$/g, "");

If i understood correctly, the Part1/Part3 are the fields the values parts[0]/parts[2] should be copied to?

For example value: "100001 | Product 1 | 23,4"

Part1/Part3 = destination fields

parts[0] = 10001

parts[2] = 23,4

Is this correct?

(sorry for my newbie questions : )

Avatar

Level 6

I think you have it right. Make sure of the following:

    1. The code should be copied into the Change event of your dropdown list.
    2. After pasting the code, the language in the upper right corner of the script window should be set to Javacript.
    3. "Part1" and "Part3" are the names of the destination fields.  You should change them to the names of the fields on your form, or rename the fields on your form to Part1 and Part3.  Case matters, so make sure the script and the field names match exactly.
    4. Make sure you save the file as a Dynamic pdf.
    5. If you're using preview in Designer, make sure the preview type is set to dynamic.

If you still have problems, please post the form if possible.

Avatar

Level 2

Thanks for the help, i just can't get it working. I changed the dynamic and other settings.

I made a demo of my try (attached: Tester.pdf)

I also change the Part* values to "Field", because i need/want to know how the code works, so i can apply to new forms.

Thanks for the patience : )

Avatar

Level 6

The script should read as follows:

var parts = xfa.event.newText.split('|');

Field1.rawValue

= parts[0].replace(/^\s+|\s+$/g, "");

Field3.rawValue

= parts[2].replace(/^\s+|\s+$/g, "");

The selected text is being split up and stored into the variable 'parts' as an array of strings.  After the first line is executed you will have:

parts[0] = '100001 '

parts[1] = ' Product 1 '

parts[2] = ' 1,1'

So, when you store the results into your fields you need to use parts[] as your source of the data.

Avatar

Level 2

Yes, now i got it working. Only one problem.

The string i have has 5 different values (Product 1 | 100001 | 5 | 7 | 23,40)

I understand that if i want a different value (for example the last one), i enter = parts[4].replace and it chooses the last one (5th)

The problem is when the value is 23,40 it show only value 23,00, so the decimals make the error somehow?

The destination values are Numeric Fields

Avatar

Correct answer by
Level 6

If the number is a decimal, try changing the field to a decimal type rather than a numeric type (although it should probably work anyway).  Also, check the display pattern to make sure it's set to display the decimal digits.

What's your locale set to?

Avatar

Level 2

When i set the destination fields format to Text Field the value comes out ok, without problems.

When the destination field is set to Numeric Field/Decimal Field the value doesn't come out at all, just 0,00 (if the Stringvalue is with decimals), but if the Stringvalue is in full numbers, the value comes out ok.

The Locale doesn't make a dirrerence.

Could the problem be var parts = xfa.event.newText.split('|'); ? Is there a way to make a newText split for number and not for Text? Because both the values are number (1 full number and 1 with 2 decimals)

Avatar

Level 2

Got the code working, i changed the Destination Field to Decimal Field and most importantly, i changed the dropdownList data (the last value) to be divided with . not with , (for example 24.0), then the total value shows up correct in the destination field (= 24,0)

Thank you for your help, stars added