Copy link to clipboard
Copied
Hi,
I have two fields in my PDF, namely:
- Last Name
- First Name
And I want to concatenate these two fields.
Example:
if the input to:
Last Name = Smith
First Name = John
I want to automatically put the "John Smith" in the third field (say Client Name).
Thanks in advance.
Copy link to clipboard
Copied
The "+" operator can concatenate two strings or one can use the concat method
Other considerations include what to do if either or both the names are not filled in..
A full solution could be using a document level script to concatenate up to 3 fields and adjust for any null fields.
// document level function;
// Concatenate 3 strings with separators where needed
function fillin(s1, s2, s3, sep = "") {
/*
Purpose: concatenate up to 3 strings with an optional separator
inputs:
s1: required input string text or empty string
s2: required input string text or empty string
s3: required input string text or empty string
sep: optional separator sting
returns:
sResult concatenated string
*/
// variable to determine how to concatenate the strings
var test = 0; // all strings null
var sResult; // re slut string to return
// force any number string to a character string for input variables
s1 = s1.toString();
s2 = s2.toString();
s3 = s3.toString();
//if(sep.toString() == undefined) sep = ''; // if sep is undefined force to null
/*
assign a binary value for each string present
so the computed value of the strings will indicate which strings are present
when converted to a binary value
*/
if (s1 != "") test += 1; // string 1 present add binary value: 001
if (s2 != "") test += 2; // string 2 present add binary value: 010
if (s3 != "") test += 4; // string 3 present add binary value: 100
/* return appropriate string combination based on
calculated test value as a binary value
*/
switch (test.toString(2)) {
case "0": // no non-empty strings passed - binary 0
sResult = "";
break;
case "1": // only string 1 present - binary 1
sResult = s1;
break;
case "10": // only string 2 present - binary 10
sResult = s2;
break;
case "11": // string 1 and 2 present - binary 10 + 1
sResult = s1 + sep + s2;
break;
case "100": // only string 3 present - binary 100
sResult = s3;
break;
case "101": // string 1 and 3 - binary 100 + 001
sResult = s1 + sep + s3;
break;
case "110": // string 2 and 3 - binary 100 + 010
sResult = s2 + sep + s3;
break;
case "111": // all 3 strings - binary 100 + 010 + 001
sResult = s1 + sep + s2 + sep + s3;
break;
default: // any missed combinations
sResult = "";
break;
}
return sResult;
}
// end document level functon;
// custom field calculation for Client Name;
var cFirstName = this.getField("First Name").value;
var cLastName = this.getField("Last Name").value;
event.value = fillin(cFirstName, cLastName, "", " ");
Copy link to clipboard
Copied
The "+" operator can concatenate two strings or one can use the concat method
Other considerations include what to do if either or both the names are not filled in..
A full solution could be using a document level script to concatenate up to 3 fields and adjust for any null fields.
// document level function;
// Concatenate 3 strings with separators where needed
function fillin(s1, s2, s3, sep = "") {
/*
Purpose: concatenate up to 3 strings with an optional separator
inputs:
s1: required input string text or empty string
s2: required input string text or empty string
s3: required input string text or empty string
sep: optional separator sting
returns:
sResult concatenated string
*/
// variable to determine how to concatenate the strings
var test = 0; // all strings null
var sResult; // re slut string to return
// force any number string to a character string for input variables
s1 = s1.toString();
s2 = s2.toString();
s3 = s3.toString();
//if(sep.toString() == undefined) sep = ''; // if sep is undefined force to null
/*
assign a binary value for each string present
so the computed value of the strings will indicate which strings are present
when converted to a binary value
*/
if (s1 != "") test += 1; // string 1 present add binary value: 001
if (s2 != "") test += 2; // string 2 present add binary value: 010
if (s3 != "") test += 4; // string 3 present add binary value: 100
/* return appropriate string combination based on
calculated test value as a binary value
*/
switch (test.toString(2)) {
case "0": // no non-empty strings passed - binary 0
sResult = "";
break;
case "1": // only string 1 present - binary 1
sResult = s1;
break;
case "10": // only string 2 present - binary 10
sResult = s2;
break;
case "11": // string 1 and 2 present - binary 10 + 1
sResult = s1 + sep + s2;
break;
case "100": // only string 3 present - binary 100
sResult = s3;
break;
case "101": // string 1 and 3 - binary 100 + 001
sResult = s1 + sep + s3;
break;
case "110": // string 2 and 3 - binary 100 + 010
sResult = s2 + sep + s3;
break;
case "111": // all 3 strings - binary 100 + 010 + 001
sResult = s1 + sep + s2 + sep + s3;
break;
default: // any missed combinations
sResult = "";
break;
}
return sResult;
}
// end document level functon;
// custom field calculation for Client Name;
var cFirstName = this.getField("First Name").value;
var cLastName = this.getField("Last Name").value;
event.value = fillin(cFirstName, cLastName, "", " ");
Copy link to clipboard
Copied
Concat Method shows an example below:
var str1 = 'Hello';
var str2 = 'World';
console.log(str1.concat(' ', str2));
// expected output: "Hello World"
console.log(str2.concat(', ', str1));
// expected output: "World, Hello"
Where do I input this script?
a) Validate > Run Custom validate script
b) Calculate > Custom calculation script
In the script provided using Concat Method, should I replace var str1 = "Hello" with var str1 = "Last Name" (the Last Name is the name of the field I used in my example). Similarly, var str2 = "World" with var str2 = "Fist Name"
And how can I use "+"? I think this solution is much easier.
Copy link to clipboard
Copied
It would go in the "Custom Calculation Script" of the Client Name field.
Both the concat method and "+" operator will result in a space in the client name if both fields are blank or the first name is blank. The use of the fillin function removes any separator characters are specified and any or all of the input values are null values.
ConcatenateText is a sample file of all three methods that reports the length of the Client Name field for each method. This non-empty result could be an issue if in a later script you are testing for completed fields or if the Client Name field is not blank.
One needs to be careful when copying scripts form the MND JavaScript or WWW3 Schools web sites since not all of the JavaScript in those sites conforms to the Acrobat JavaScript extensions. The "console.log()" method is replaced by "console.println()" method in Acrobat JavaScript. So the example script needs to read:
var str1 = 'Hello';
var str2 = 'World';
console.println(str1.concat(' ', str2));
// expected output: "Hello World"
console.println(str2.concat(', ', str1));
// expected output: "World, Hello"
Copy link to clipboard
Copied
Here's the most simple way of doing it. Use this code as the custom calculation script of "Client Name":
event.value = this.getField("First Name").valueAsString + " " + this.getField("Last Name").valueAsString;
Copy link to clipboard
Copied
This worked great for me - I needed to auto-populate a user name from the First Name and Last Name with a period "." in between so I modified it slightly in the Custom calculation script: [[ event.value = this.getField("First Name").valueAsString + "." + this.getField("Last Name").valueAsString ]] and it came out beautifully "FirstName.LastName"
Thank you for your help!
Copy link to clipboard
Copied
Thank you very much!! It's just what I needed 🙂
Copy link to clipboard
Copied
This one's the ticket right here -- when you just need to join the contents of other fields into another, this way is the simplest.
Copy link to clipboard
Copied
Thank you so much for providing solution to my issue.
Copy link to clipboard
Copied
This information helped me solve a problem on a form I was creating for which it required a reference number to populate. The reference number had to be all one field that included specific verbiage ("REF NUM: ") to be tied to user-entered information (NPI). I finally accomplished this using the custom calculation method, but just inserting the required verbiage into the equation rather than pulling the value of another field. When I modified the script to the below and the field started functioning as expected (after 3 days of Google searches and trial and error with concatenation, and numerous incredibly disgruntled growls), I let out a hoot and holler!
event.value = "REF NUM: " + this.getField("NPI").valueAsString;
Copy link to clipboard
Copied
What if I want to concatenate the value of one field.
For instance if [Case/Investigating Officer] = Thomas Moore I would like to automatically populate the [EMAIL] field with tmoore@url.com.

