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

How to concatenate two input fields?

Contributor ,
Mar 01, 2019 Mar 01, 2019

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.

TOPICS
PDF forms
21.1K
Translate
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
1 ACCEPTED SOLUTION
LEGEND ,
Mar 01, 2019 Mar 01, 2019

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, "", " ");

View solution in original post

Translate
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 ,
Mar 01, 2019 Mar 01, 2019

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, "", " ");

Translate
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
Contributor ,
Mar 01, 2019 Mar 01, 2019

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.

Translate
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 ,
Mar 01, 2019 Mar 01, 2019

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"

Translate
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 ,
Mar 01, 2019 Mar 01, 2019

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;

Translate
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
New Here ,
Jul 08, 2020 Jul 08, 2020

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!

Translate
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
New Here ,
May 02, 2021 May 02, 2021

Thank you very much!! It's just what I needed 🙂

 

Translate
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
Contributor ,
Feb 24, 2022 Feb 24, 2022
LATEST

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.

Translate
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
Contributor ,
Mar 01, 2019 Mar 01, 2019

Thank you so much for providing solution to my issue.

Translate
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 Beginner ,
Sep 24, 2019 Sep 24, 2019

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;

Translate
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
New Here ,
Jan 13, 2021 Jan 13, 2021

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.

Translate
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