Jump to content

Formatting 9 Digit Zipcodes


INGuser

Recommended Posts

Good afternoon,

 

I am trying to format zipcodes within my project. the data file is a mix of 5 and 9 digit zipcodes. I have been using the following in a empty text rule but this is not working on the zipcodes that are missing the leading zeros. Any help is greatly appreciated.

 

New Empty Text Rule

return Field("ZIPCode").replace(/^(\d{5})-?(\d{4})$/, "$1-$2");

Link to comment
Share on other sites

Since your leading zeros are dropping, your regular expression is failing to capture the pattern. In your replace function, you are looking for a series of 5 digits, a hyphen (optionally), followed by a series of 4 digits.

 

You can try checking the zip code's length and adding the leading zeros back to it conditionally. Basically, checking to see if the zip is less than 5 digits. If it is, add leading zeros until the length of the zip is 5. If it's greater than 5 digits, add leading zeros until the length is 9. At that point your current replace function should work. Here's an example:

// Assign this to your FP Field
var zip = '345-1234'; // Zip Code: 00345-1234

// Remove anything that's not a digit
zip = zip.replace(/[^\d]/g,''); 

// If the zip code is less than 5 digits, add leading zeros to make its length 5
// If it's greater than 5, add leading zeros to make its length 9
var stop = (zip.length <= 5) ? 5 : 9;
while (zip.length < stop){
   zip = '0' + zip;
}

return zip.replace(/^(\d{5})-?(\d{4})$/, "$1-$2");

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...