I have a data file with the ZIP being 5 to 9 digits. I need to format this to place a hyphen if 9 after the 5 digit so outcome is 5-4 or if 5 no -
The ZIP is setup in my address block
I have a data file with the ZIP being 5 to 9 digits. I need to format this to place a hyphen if 9 after the 5 digit so outcome is 5-4 or if 5 no -
The ZIP is setup in my address block
Hi NMjoy,
You can do this in the data mapper using regex. Select your field and change the Mode to JavaScript. You will have a string similar to this
data.extract(1,10,0,1,"<br />");
Add .trim() to the end of it like I did below and replace mine with yours after the var zip =.
var zip = data.extract(1,10,0,1,"<br />").trim(); //add your zip field data.extract here
if(zip.length == "9"){
zip = zip.replace(/(\d{5})/, "$1-");
}else{
zip;
}
I wanted to upload my data mapper here but the upload option only allows images. I will ask OL how to upload other files.
EDIT: That was painful. I had to use preformatted text option otherwise the break char makes a new line.
EDIT2: I have corrected the code to add - after the fifth digit. My bad.
Regards,
S
β¦ or, in a more concise manner, select your field as you normally would and add the following code to the Post function:
trim().replace(/(\d{5})/gi,"$1-").replace(/-$/gi,"");
The trim() removes any blank spaces at the end of your selection, the first replace() adds a dash after the first 5 digits and the second replace() removes any dangling dashes in case the selection only contains 5 digits.
Trying to apply this in the datamapper
Receive error trim not defined.
data.extract(βZIPβ,0);trim().replace(/(\d{5})/gi,β$1-β).replace(/-$/gi,ββ);
Field list ZIP
Mode - JavaScript
Expression - inserted above
Type - String
As I stated, you can extract your data normally (i.e. with the extraction mode set to location), instead of setting the extraction mode to JavaScript. Then you can copy my code in the Post function parameter. Hereβs what it looks like:
Thank you!!! It worked, learning and this will make my life much easier!!!