9 Replies Latest reply on Aug 21, 2009 7:29 PM by wreded

    Concatenation Problem

    wreded

      Hi,

      i'm having an issue with concatenating 4 string fields into 2 variables.  User fills out form LastName; FirstName; MI; Suffix (Jr., Sr., etc.).  i grab these with an onblur event and rebuild the names to a 'reverse name' and a 'full name'.  If the Suffix is empty (< 1) don't add commas or the Suffix.  If it has an entry add commas.  My problem is that it always adds commas.  i can't figure out why.

      Code looks like this:

      =================================


      var vRevName = getField("revname");              //var p = getField("revname");
      var vReqEmail = getField("reqemail");            //var q = getField("reqemail");
      var vLname = getField("Lname");                  //var r = getField("Lname");
      var vFname = getField("Fname");                  //var s = getField("Fname");
      var vMI = getField("MI");                        //var t = getField("MI");
      var vSuffix = getField("Suffix");                //var u = getField("Suffix");
      var vRank = getField("Rank");                    //var v = getField("Rank");
      var vFullName = getField("fullname");            //var w = getField("fullname");

      //

      //Create e-mail name from typed name
      //
      if (vRank.value != 'Ctr') {
         vReqEmail.value = vFname.value + '.' + vLname.value +
      '@abc.com';}
      else {
         vReqEmail.value =  vFname.value + '.' + vLname.value +
      '.ctr@abc.com'.ctr@abc.com';}
      //
      '

      //Create 'reverse' and 'full' name from typed name
      if (vSuffix.length < 2) {
          vRevName.value = vLname.value + ', ' + vFname.value + ' ' + vMI.value + '., ' + vSuffix.value;
          vFullName.value = vLname.value + ', ' + vFname.value + ' ' + vMI.value + '., ' + vSuffix.value + ', ' + vRank.value;}
      else {
          vRevName.value = vLname.value + ', ' + vFname.value + ' ' + vMI.value + '.';
          vFullName.value = vLname.value + ', ' + vFname.value + ' ' + vMI.value + ', ' + vRank.value;}

      =================================

      i've tried testing the vSuffix for blank, < 1, > 0, and every permutation i can think of but even with NO suffix i still get a vRevName and vFullName with commas where i don't want them.

       

      vRevName = Hominygrits, Hojo B., , CMSgt

      vFullName = Hominygrits, Hojo B.,

       

      i'm sure it's something i'm overlooking, but can't see.

      Any ideas?

      Thanks,

      Dave

        • 1. Re: Concatenation Problem
          try67 MVP & Adobe Community Professional

          You're checking the length of vSuffix instead of vSuffix.value ...

          1 person found this helpful
          • 2. Re: Concatenation Problem
            wreded Level 1

            Ok, now that i check vSuffix.value.length i only get the errors i introduced in my logic.

            Thanks for Your answer to my question.

            Dave

            • 3. Re: Concatenation Problem
              gkaiseril MVP & Adobe Community Professional

              You also might consider converting the field values to strings just in case the field is mistakenly assumed to be a number.

               

              I would also use the object reference 'this' and use the 'this.getField(...).value = ...' when setting a field value.

              1 person found this helpful
              • 4. Re: Concatenation Problem
                wreded Level 1

                i have never really understood the dot '.' notation thing; and 'this.' really puts me in a tailspin.  i'll give 'er a try and maybe someday i'll come to understand.

                Thanks,

                Dave

                • 5. Re: Concatenation Problem
                  gkaiseril MVP & Adobe Community Professional

                  The single dot '.' marks the end of an object, property or method. So in the code string 'var vRank = this.getField("Rank").value.toString()", the first dot marks the end of the referenced object, "this", the next dot after 'getField("Rank")' marks the end of the method to get a filed object and the dot after the 'value' marks the end of the value property. It is a way of compressing coding so you do not need to create a series of variables to hold each object of value. If you wrote the string without the dots it could be a variable name, not a good coding practice.

                   

                  One could write the above as follows

                   

                  var oRank = this.getField("Rank"); // get the field object
                  var vRank = oRank.value; // get the value of the Rank field object
                  var sRank = vRand.toString(); // convert the value to a string

                   

                  Acrobat is not known for good memory management and having a larger number of variables can sometimes cause confusion to JavaScript and the coder.

                  • 6. Re: Concatenation Problem
                    try67 MVP & Adobe Community Professional

                    There's no need to convert the value of a field to string. Instead just use

                    the property 'valueAsString'.

                    It's also better becuase if the value is a number with leading zeros

                    ("000123"), you will lose those zeros ("123") by accessing the 'value'

                    property, unless you access 'valueAsString'.

                    • 7. Re: Concatenation Problem
                      gkaiseril MVP & Adobe Community Professional

                      One may also need to deal with missing name fields or a no value field like the MI or Suffix and having to adjust for the missing not use the following separator for the missing data

                       

                      This can be addressed by document level functions to concatenate the fields and add the separater as required. And then create a function to build the necessary strings form the forms fields.

                       

                      // document level scripts

                      // Concatenate up to 3 strings with an optional separator 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 string

                      returns:
                      sResult concatenated string
                      */

                      // variable to determine how to concatenate the strings
                        var test = 0; // all strings null
                        var sResult; // result 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 is missing - null the sep parameter
                        if (sep == undefined) sep = '';
                      /*
                      assign a binary value for each string present
                      and adds this value to compute a value used to
                      determine which action to be taken.

                      The value is computed using a binary representation for present(1) and not present(0).
                      */
                        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 string
                        */
                        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 1 + 10
                           sResult = s1 +  sep +  s2;
                        break;

                        case "100": // only string 3 present - binary 100
                           sResult = s3;
                        break;

                        case "101": // string 1 and 3 - binary 1 + 100
                           sResult = s1 + sep +  s3;
                        break;

                        case "110": // string 2 and 3 - binary 10 + 100
                           sResult = s2 + sep + s3;
                        break;

                        case "111": // all 3 strings  - binary 1 + 010 + 100
                           sResult = s1 + sep + s2 + sep + s3;
                        break;

                        default: // any missed combinations
                           sResult = "";
                        break;
                      }

                      return sResult;
                      }

                      function BuildNames() {
                      var vLname = this.getField("Lname").value;                  //var r = getField("Lname");
                      var vFname = this.getField("Fname").value;                  //var s = getField("Fname");
                      var vMI = this.getField("MI").value;                        //var t = getField("MI");
                      if (vMI != '') vMI +='.';  // add ".'
                      var vSuffix = this.getField("Suffix").value;                //var u = getField("Suffix");
                      var vRank = this.getField("Rank").value;                    //var v = getField("Rank");
                      var vReqEmail = '';            //var q = getField("reqemail");
                      var vFullName = '';
                      var vRevName = '';

                      // minimum field names for building concatenated strings
                      if(vRank != '' & vFname != '' & vLname != '') {
                      console.println(vRank + ' '  + vFname + ' ' + vMI + ' ' + vLname + ' ' + vSuffix);
                      //Create e-mail name from typed name
                      if (vRank != 'Ctr') {
                         // First.Last
                         vReqEmail = fillin(vFname, vLname, '', '.');
                         // (First.Last)@abc.com
                         vReqEmail = fillin(vReqEmail,'@abc.com', '', '');
                         } else {
                         // First.last.ctr@abc.com
                         vReqEmail =  fillin(vFname, vLname, 'ctr@abc.com', '.');
                         }

                      //Create 'full' name from typed name
                      // First MI Last
                      vFullName = fillin(vFname, vMI, vLname, ' ');
                      // (First MI Last), Suffix, Rank
                      vFullName = fillin(vFullName, vSuffix, vRank, ', ');
                      // Create RevName
                      // First MI   
                      vRevName = fillin(vFname, vMI, '', ' ');
                      // Last, (First MI), Suffix
                      vRevName = fillin(vLname, vRevName, vSuffix, ', ');
                      // (Last, First MI, Suffix), Rank
                      vRevName = fillin(vRevName, vRank, '', ', ');
                      }
                      // fill in fields with built strings
                      this.getField("reqemail").value = vReqEmail;
                      this.getField("fullname").value = vFullName;
                      this.getField("revname").value = vRevName;
                      return;
                      }

                      // end of doucment level scripts

                       

                      Then one only needs to call the "BuildNames()" function in each of the input name and rank fields. The fields will fillin when the minimum amount of data is present and adjust for a missing MI. One would use the following scirpt in the "On Blur" action:

                       

                      // fillin the name fields

                      BuildNames();

                      • 8. Re: Concatenation Problem
                        wreded Level 1

                        Holy Schmoly!!!!  It's gonna take me a month to wrap my brain around all this!!

                        i can't believe You guys, i come with what i think is a simple problem and i get all these elegant solutions and instruction on dot notation too!

                        This is simply the BEST board i've ever come across.

                        Many, many thanks!!

                        Dave

                        • 9. Re: Concatenation Problem
                          wreded Level 1

                          That is TOO COOL gKaiseril!

                          i prefer functions to straight code, though i end up writing straight code more often than not due to a lack of time and brain power.

                          Gotta try this out immediately!!

                          Thanks,

                          Dave