Photo by Yannik Mika on Unsplash

your ZIP codes

Use RegexExtract to validate the zip value

Christiaan Huizer
3 min readApr 18, 2024

--

A short blog on how to validate USA ZIP codes. It is a based on a public question. As European I had no clue about the logic behind this system, luckily on on Wikipedia we find.

A ZIP Code (an acronym for Zone Improvement Plan[1]) is a system of postal codes used by the United States Postal Service (USPS). The term ZIP was chosen to suggest that the mail travels more efficiently and quickly[2] (zipping along) when senders use the code in the postal address.

Introduced on July 1, 1963, the basic format comprised five digits.[3] In 1983, an extended code was introduced named ZIP+4 ; it included the five digits of the ZIP Code, followed by a hyphen and four digits that designated a more specific location.

ZIP Code is a trademark and ZIP+4 is a registered trademark of the United States Postal Service, which also registered ZIP Code as a service mark until 1997.[4]

The basics

We have two options:

  • 5 numbers
  • 5 numbers a hyphen and 4 numbers

The regex

The validation needs to check these criteria. My first idea was to get a regex and since this worked in minutes I stick to it. This is what I got.

^\d{5}(?:[-\s]\d{4})?$

Let’s break down the regex:

  • ^: Matches the beginning of the string.
  • \d{5}: Matches exactly five digits.
  • (?: ... )?: This is a non-capturing group that makes the hyphen and the following four digits optional.
  • -: Matches a hyphen.
  • \s: Matches a whitespace character (space or tab).
  • \d{4}: Matches exactly four digits.
  • $: Matches the end of the string.

This regex will match both basic five-digit ZIP codes (e.g., 12345) and ZIP+4 codes (e.g., 12345–6789).

Here are some things to keep in mind:

  • This regex only validates the format, not the existence of the ZIP code itself.
  • ZIP codes can have leading zeros (e.g., 00972). The regex will accept them as valid formats.
RegexExtract Applied
.RegexExtract("^\d{5}(?:[-\s]\d{4})?$","gi")

Alternatives

When you are not familiair with Regex you can make a few manipulations I did not check:

  • the length( ) should be 5 or 10
  • numbers only (5) or numbers (9) with one hyphen
  • use Split(), IsNumber(), Filter() and Join()
  • when lenght() greater than 5, position 6 should be a hyphen, apply Find()

As you see, you can solve this puzzle without Regex, but it will complicate the matter unnecessarily. Apply the regex and your are set.

You can work with one error message that explains the base logic. As said a short blog, so it ends here.

My name is Christiaan and blog about

. Since the summer of 2023 often (but not only) about how to with AI to support organisations dealing with texts and templates. The latest major Coda AI update was on Dec 7, 2023. With the announcement of Snowflake as partner on April 10, I expect to see a new AI logic put in place before the summer holidays.

Why I focus on

AI you can read here: ⤵️

I hope you enjoyed this article. If you have questions feel free to reach out. Though this article is for free, my work (including advice) won’t be, but there is always room for a chat to see what can be done. You find my (for free) contributions to the Coda Community and on Twitter.

Coda comes with a set of building blocks ー like pages for infinite depth, tables that talk to each other, and buttons that take action inside or outside your doc ーso anyone can make a doc as powerful as an app (source).

Not to forget: the Coda Community provides great insights for free once you add a sample doc.

--

--

Christiaan Huizer

I write about how to Coda . You find blogs for beginners and experienced makers. I publish about 1 / week. Welcome!