Skip to content

Instantly share code, notes, and snippets.

@gowon
Last active January 9, 2019 16:59
Show Gist options
  • Save gowon/dd69cb17827c2f69e0e6fb2752864310 to your computer and use it in GitHub Desktop.
Save gowon/dd69cb17827c2f69e0e6fb2752864310 to your computer and use it in GitHub Desktop.
US Zip Code Regex Validation

Strict

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

StrictFive

^(\d{5})(?:\-\d{0,3}|\-?(\d{4}))?$

Loose

^(\d{5})(?:\-?\d{0,3}|\-?(\d{4}))?$
Zip Code Value Strict Valid Strict Outcome StrictFive Valid StrictFive Outcome Loose Valid Loose Outcome
1 N N N
12 N N N
123 N N N
1234 N N N
12345 Y 12345 Y 12345 Y 12345
123456 N N Y 12345
1234567 N N Y 12345
12345678 N N Y 12345
123456789 Y 123456789 Y 123456789 Y 123456789
12345- N Y 12345 Y 12345
12345-6 N Y 13245 Y 12345
12345-67 N Y 12345 Y 12345
12345-678 N Y 13245 Y 12345
12345-6789 Y 123456789 Y 123456789 Y 123456789
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment