Skip navigation
Neil-Kentucky
Currently Being Moderated

Formatting Address Fields into one

Jul 18, 2012 12:04 PM

Tags: #acrobat #x #forms

Using Acrobat X PRO:

 

I have a multi-page document in which a user is prompted for the address in two different formats:

 

1) With the address split into 4 different values/fields:

 

Street Field, City Field, State Field, Zip Field

 

Example: "1234 Count Rd" (Next Line) "St Louis", "MO"  "55555"

 

2) With the address all together:

 

Complete Address on one line with a maximum length of 40 Characters.

 

Example: 1234 Count Rd, St. Louis, MO 55555

 

---

 

I would like users to be able to Fill in the Street, City, State, and Zip fields, and no matter what the format of the following fields, have the data autopopulated.

 

This isn't a problem for example #1.  However, with #2 if i place all fields close together, I have to guess on how far apart to space each field and still have it fit and format properly.

 

Is there any way to combine the values of 4 fields into one value (separated by commas)?

 

Making the address field 'auto-fit' may be necessary but it's ugly if there is extra space in the remaining fields. 

 

PLEASE SEE THIS EXAMPLE document i have created for the purpose of this post

 
Replies
  • George Johnson
    9,208 posts
    Aug 11, 2002
    Currently Being Moderated
    Jul 18, 2012 12:44 PM   in reply to Neil-Kentucky

    If you want to concatenate the four values into one, the custom Calculate script for the full address field can be something like:

     

    // Custom Calculate script for full address field

    (function () {

     

        // Get the field values and place in array

        var aa = [];

     

        var street = getField("Street").valueAsString;

        if (street) aa.push(street);

     

        var city = getField("City").valueAsString;

        if (city) aa.push (city);

     

        var state = getField("State").valueAsString;

        if (state) aa.push (state);

     

        var zipcode = getField("Zip Code").valueAsString;

        if (zipcode) aa.push (zipcode);

     

        // Concatenate and set this field value

        event.value = aa.join(", ");

     

    })();

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 18, 2012 12:44 PM   in reply to Neil-Kentucky

    And if the combined fields are longer than 40 characters?

     

    With Acrobat 4 there was a sample form that one entered the data in fields like tile, first name, mi, last name, abd degrees  and then created a full name from those individual fields and adjusted for missing data.

     

    The doucment 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;
    }

     

     

    And then one could use a custom calculation script for a full address field:

     

    // Full business address including country

    function doFullBusinessAddress() {
      var ba = this.getField("business.address.full");
      var bc = this.getField("business.address.citystatezip");
      var bu = this.getField("business.address.country");

      event.value = fillin(ba.value, bc.value, bu.value, ", ");
    }

    doFullBusinessAddress();

     
    |
    Mark as:
  • George Johnson
    9,208 posts
    Aug 11, 2002
    Currently Being Moderated
    Jul 18, 2012 2:49 PM   in reply to Neil-Kentucky

    That's interesting, because I tested it using the form you posted and it works fine. I'd be happy to take a look at your new form if you send it to me at: acroscript at gmail dot com

     
    |
    Mark as:
  • George Johnson
    9,208 posts
    Aug 11, 2002
    Currently Being Moderated
    Jul 18, 2012 3:09 PM   in reply to Neil-Kentucky

    Yes, it's all set up for that. You can add the following function to a document-level JavaScript:

     

    // Include in a document-level JavaScript

    function concatAddress() {

     

        // Get the field values and place in array

        var aa = [];

     

        var street = getField("Street").valueAsString;

        if (street) aa.push(street);

     

        var city = getField("City").valueAsString;

        if (city) aa.push (city);

     

        var state = getField("State").valueAsString;

        if (state) aa.push (state);

     

        var zipcode = getField("Zip Code").valueAsString;

        if (zipcode) aa.push (zipcode);

     

        // Concatenate and set this field value

        event.value = aa.join(", ");

     

    }

     

    And call it from the calculate event of any field that you want to populate with the full address:

     

    // Custom calculate script

    concatAddress();

     

    The full address fields do not need to be named the same, but they will need to call the same function in their calculate event. You can change the name of the function if you like.

     
    |
    Mark as:
  • George Johnson
    9,208 posts
    Aug 11, 2002
    Currently Being Moderated
    Jul 18, 2012 3:32 PM   in reply to Neil-Kentucky

    I'd have to see your form.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points