2 Replies Latest reply on Apr 25, 2013 9:08 AM by gkaiseril

    combine two fields and if not blank add hyphen

    1963cja

      I have just started to use Acrobat x Standard and know only the basics. 

       

      I have two address fields SuiteApt and AddressLn1. Once the user adds data to either of these fields they are linked to another field (MailAddress)that puts them together for a combined mailing address.

       

      What I’m trying to do is have a HYPHEN added if a SuiteApt is typed and no hypen if only AddressLn1 has data.

       

      Example"

       

      if SuiteApt is 123 and AddressLn1 is 456 King St W

       

      123 - 456 King St W

       

      or

       

      if SuiteApt is blank and AddressLn1 is 456 King St W

       

      456 King St W

       

      Any help is appreciated.

       

      Cheers

        • 1. Re: combine two fields and if not blank add hyphen
          try67 MVP & Adobe Community Professional

          Use this as the custom calculation code for MailAddress:

           

          var s1 = this.getField("SuiteApt").valueAsString;

          var s2 = this.getField("AddressLn1").valueAsString;

          if (s1!="" && s2!="") event.value = s1 + " - " + s2;

          else event.value = s1 + s2;

          • 2. Re: combine two fields and if not blank add hyphen
            gkaiseril MVP & Adobe Community Professional

            Have you looked at the samples provided with the Acrobat distribution CD/DVD?

             

            There is a sample provided in the PFN folder that has a function named "fillin" that can combine up to 3 fields with an optional separator is there are 2 or more non-null fields.

             

            // Concatenate up to 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;
            }

             

            event.value = fillin(this.getField("SuiteApt").valueAsString, this.getField("AddressLn1").valueAsString, "", "-");