Jump to content

Need help with a city State Zip rule


JHester

Recommended Posts

Needing help with a rule that does the following:

For the city it needs to Force UC the first letter and force LC the remaining letters in the city with a comma coming directly after.

For the state it needs to Force UC the state initials with no comma after the state and before the zip.

 

Final format should look like this:

Cordova, TN 38018

 

I am still pretty new at all this so any help would be greatly appreciated.

 

-Jason

Link to comment
Share on other sites

JHester

Here is one way to do this. There are default text rules that will convert a field to proper case and all caps. You can use one of these rules for the city and one for the state. As far as the comma, you can create a new empty text rule and make a simple if then statement. If the state is not equal to nothing then return a comma and a space if there is nothing in the state field the return nothing. I'll attach a couple of screenshots with this. Good luck.

Picture2.png.b1ee45ebdbf18e42d876c294d0785d5c.png

Picture5.png.f9b76513be36bd2d546409cb326dd8a4.png

Link to comment
Share on other sites

This will format the fields as you have requested, however it won't take into account scenarios where the "city" field is blank and leaving you with something that looks like this: ", TN 55555".

 

return ToTitleCase(Field("City")) + ", " + ToUpper(Field("State")) + Field("Zip");

 

To address that issue try the following:

 

return (Field("City") != "") ? ToTitleCase(Field("City")) + ", " + ToUpper(Field("State")) + Field("Zip") : Field("Zip");

 

With the above code, it will only return the zip code if the city field is blank.

Link to comment
Share on other sites

  • 5 years later...
any advice on this if they are all in one field and you need to do the same thing?

CITY, STATE ZIP is the filed name.

That's the name of the field, or its format?

 

If the state name is spelled out, instead of a two-letter abbreviation, then you can simply apply ToTitleCase to the whole thing, like so:

return ToTitleCase(Field("CITY, STATE ZIP"));

If it has a two-letter state abbreviation, then this should work:

return Field("CITY, STATE ZIP").replace(/^(.*),\s*(\w{2})\s+([\d\-]+)$/, function(all,city,state,zip){ return ToTitleCase(city) + ", " + ToUpper(state) + " " + zip; });

If the state name can either be spelled out, or a two-letter abbreviation, you can use this code:

return Field("CITY, STATE ZIP").replace(/^(.*),\s*(\w+)\s+([\d\-]+)$/, function(all,city,state,zip){ return ToTitleCase(city) + ", " + (state.length > 2 ? ToTitleCase(state) : ToUpper(state)) + " " + zip; });

However, there are several caveats here, based on common but incorrect assumptions. The first one is that not every proper name follows the "first letter uppercase, all other letters lowercase" rule, and this includes many US city names. For instance, this won't work correctly with a city such as McKinney, TX. It also won't work with military addresses, which don't have a city name. It might not work correctly with non-US addresses at all. Etc. etc. This is one of the reasons why the USPS standardizes all addresses with all caps.

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...