Copy link to clipboard
Copied
For example, I have one textbox containing "City, State Zip" that I would like to be separated into 3 separate boxes. Can someone help me with a javascript for this?
Copy link to clipboard
Copied
In that case, try these codes:
// City custom calculation script
var address = this.getField("Address").valueAsString;
if (address=="") event.value = "";
else event.value = address.split(", ")[0];
// State custom calculation script
var address = this.getField("Address").valueAsString;
if (address=="") event.value = "";
else event.value = address.split(", ")[1].replace(/\d/g, "");
// Zip custom calculation script
var address = this.getField("Address").valueAsString;
if (address=="") event.value = "";
else event.value = address.split(", ")[1].replace(/\D/g, "");
Copy link to clipboard
Copied
It can only be done if you can describe clear rules on to to perform the split.
For example, is there always a comma after the City value? Does the zip code follow a specific pattern? If so, it will be possible to identify them and split the string into individual parts. But if the text is not consistent it will be quite difficult to do so...
Copy link to clipboard
Copied
Yes, the format is always City, State Zip. The state is abbreviated. Example "Nashville, TN 37221".
I am importing the data from an excel spreadsheet into a form. The program the populates the spreadsheet puts all this info into one column. The form has the data in 3 separate blanks.
Copy link to clipboard
Copied
In that case, try these codes:
// City custom calculation script
var address = this.getField("Address").valueAsString;
if (address=="") event.value = "";
else event.value = address.split(", ")[0];
// State custom calculation script
var address = this.getField("Address").valueAsString;
if (address=="") event.value = "";
else event.value = address.split(", ")[1].replace(/\d/g, "");
// Zip custom calculation script
var address = this.getField("Address").valueAsString;
if (address=="") event.value = "";
else event.value = address.split(", ")[1].replace(/\D/g, "");
Copy link to clipboard
Copied
worked perfectly! Thanks so much
Copy link to clipboard
Copied
There are many cases where this code might not work.
1. Cities with split names like Los Angeles or San Francisco.
2. Use of the Zip+4 Zip code 60130-001 or 60130 1234
Copy link to clipboard
Copied
1. My code will handle that just fine.
2. I had assumed the zip code contains only numbers. If that's not the case then it will indeed cause some issues, although in the cases you described above all that will happen is that the result will be one long number...
Copy link to clipboard
Copied
Since the Zip Code + 4 is a defined standard of the United State Postal Service and service can be refused if the that standard is not meet, this might be an important requirement.
Acrobat has 2 options for the U.S. Zip codes under the Special Format. One is for the 5 digit and the other is for the Zip plus 4 which adds the "-" and the 4 digit route code.
The U.S. Zip code is far simpler than the U.K. postal code and I have posted code that will validate the UK postal code including excluding many of the unused letters.
I also see that if the "," is omitted from the address string being passed from Excel there will be an error condition raised with the script.
It is possible using the RegExp object to handle all of these options or one could write a user function that can examine the strings construction and then make the necessary steps to split the text.
Excel does not have a lot of good input editing tools unless one write some VBA macros so this could become a real mess.
Copy link to clipboard
Copied
I specified to try67 in an earlier question that the zip only needed to be the 5 dig code without the +4 so he/she did take that into consideration initially. So far the code has worked as requested, but thank you for reviewing it as I have learned in my very limited knowledge of javascript there are "many ways to skin a cat" and that one little typo can throw the whole thing off.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more