INGuser Posted January 29, 2015 Share Posted January 29, 2015 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"); Quote Link to comment Share on other sites More sharing options...
step Posted January 29, 2015 Share Posted January 29, 2015 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"); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.