8 Replies Latest reply on Nov 23, 2011 5:56 PM by stealthrs

    Meshing together text from 3 other boxes (Easy)

    stealthrs Level 1

      This one should be easy.  I have 4 textfields:

       

      first_name

      middle_initial

      last_name

      full_name

       

      All I want to do is add first_name, middle_initial, last_name ---> into ---> full_name

       

       

      P.S.  I do try to search before posting, but I had difficulty wording this to get what I was looking for.

       

      Thanks.

        • 1. Re: Meshing together text from 3 other boxes (Easy)
          George_Johnson MVP & Adobe Community Professional

          There are a number of ways to do this. Here's one way that is intended to be the custom calculate script for the full_name field:

           

           

          // Custom calculate script for full_name field
          (function () {
          
              // Get the field values
              var nf = getField("first_name").valueAsString;
              var nm = getField("middle_initial").valueAsString;
              var nl = getField("last_name").valueAsString;
          
              // Start off with an empty array
              var fna = [];  // full name array
          
              // Add names as items to array, if they exist
              if (nf) fna.push(nf);
              if (nm) fna.push(nm);
              if (nl) fna.push(nl);
          
              // Create full name from array, adding spaces between names
              var full_name = "";
              if (fna.length) full_name = fna.join(" ");
          
              // Set this field value to the full name
              event.value = full_name;
          
          })();
          

           

          The full_name field will be updated as the user enters values in each of the 3 input fields. The first and last lines are there to prevent the unnecessary creation of document-global variables.

          1 person found this helpful
          • 2. Re: Meshing together text from 3 other boxes (Easy)
            stealthrs Level 1

            I copy-pasted the code and did a little test document and it didn't work.... I'm so new at scripting that I'm going to have to go in that 700 page Adobe JavaScript book and figure out what most of that stuff means, but I'll try to learn this stuff.

             

            https://acrobat.com/#d=SRKj8ZMfuUctl5PRJs5-Gg

             

            Is there maybe a space in there that shouldn't be that's preventing this from working?

             

            Thanks for your help.

            • 3. Re: Meshing together text from 3 other boxes (Easy)
              try67 MVP & Adobe Community Professional

              You placed the code in the wrong place. It's a custom calclation code, not a MouseUp action.

              • 4. Re: Meshing together text from 3 other boxes (Easy)
                BarlaeDC Level 4

                Hi,

                 

                There is nothing wrong with the script, it is just placed in the wrong place and hence it not called when expected.

                 

                The script is currently placed in MouseUp action of the Full name field.

                 

                If you move this script from there and place it in the Calculate custom script location then it works as expected.

                 

                To get to the Calculate custome script location :-

                 

                - open the preferences of the full name field

                - Select the "calculate" tab

                - Click the last radio button named "Custom calculation script"

                - Paste the script in the window that opens

                - click "ok"

                - click "close"

                 

                And it should work as you expect.

                 

                Hope this helps

                 

                Malcolm

                • 6. Re: Meshing together text from 3 other boxes (Easy)
                  gkaiseril MVP & Adobe Community Professional

                  I prefer to use a docuemnt level script that will concatenate upto 3 fields along with an optional seprator and adjust spacing for null values of any field.

                   

                  The document level script:

                   

                  // 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;
                  }

                   

                  The custom calculation script for the Full_Name becomes:

                   

                  // Name only

                  function doFullName() {
                    var first  = this.getField("first_name");
                    var initial= this.getField("middle_initial");
                    var last   = this.getField("last_name");

                    event.value = fillin(first.value, initial.value, last.value, " ");
                  }

                  doFullName();

                   

                  The above process can be used to build phone numbers, addresses, and date strings from individual field and enter special sperators as needed.

                  • 7. Re: Meshing together text from 3 other boxes (Easy)
                    try67 MVP & Adobe Community Professional

                    These are all great scripts, but you can achieve what the OP wanted to do with a much more simple code:

                     

                    var combinedName = getField("first_name").value + " " + getField("middle_initial").value + " " + getField("last_name").value; // concatenate the fields

                    combinedName = combinedName.replace(/^\s*/, "").replace(/\s*$/, "").replace(/\s+/g, " "); // remove all unwanted spaces

                    event.value = combinedName; // apply the combined value to this field

                    • 8. Re: Meshing together text from 3 other boxes (Easy)
                      stealthrs Level 1

                      try67's and George's codes worked the best and seemed to work identically, although written differently.  Try's is also a tad bit shorter

                       

                      I appreciate it!