These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Populating fields

Peter at the Fr...
Registered: Jul 4 2011
Posts: 1

Hi,
 
I need to populate a field with a value consisting of a combination of values from 3 fields.
 
1] Street address (123 Main Street)
2] City (Anytown)
3] State (CO)
 
New field
A combination of the 3 with extra spaces removed and a comma inserted after the state
 
I am thinking I would need to collect the 3 field values to memory variables, create the new variable and write it to the 4th field.
 
Thank you in advance,
Peter

My Product Information:
Acrobat Pro 10.0, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You don't need a variable, just use a script like this as the custom calculation event of 4th field:
event.value = getField("Street address").value + " " getField("City").value + ", " + getField("State").value;

If you want line-breaks in between the lines, use "\n" instead of the space and comma I've added.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
I use a document level function like:

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

Which only add the optional separator when need and if all input fields are null returns a null string so if you are using a null string for some test elsewhere in the form, that test will still work properly.

You can fill in the full address with a custom calculation script like:

// get street address
var sStreet = this.getField('Street address').value;
// get city
var sCity = this.getField('City').value;
// get state
var sState = this.getField('State').value;
// build string
//streed city
event.value = fillin(sStreet, sCity, sState,"");
// add coma is state not null
if(sState != "") event.value +=","


If you want a multi line text field with the zip code like a mailing label your custom calculation script would be::

// get street address
var sStreet = this.getField('Street address').value;
// get city
var sCity = this.getField('City').value;
// get state
var sState = this.getField('State').value;
// get the zip code - use as string to preserve leading zeros
var sZip = this.getField('Zip').valueAsString;

// build city state string with ", " separator as needed
var sCityStateZip = fillin(sCity, sState, "", ", ");
// add zip code with ' ' separator
sCityStateZip = fillin(sCityStateZip, sZip, "", " ");
// add street address with a new line separator
event.value = fillin(sStreet, sCityStateZip, "", "\n");

George Kaiser