Regular Expressions 101

Community Patterns

US Zipcode Validator

1

Regular Expression
PCRE (PHP <7.3)

/
^(?:[1-9]|0(?!0{4}))\d{4}(?:[-\s]\d{4})?$
/
ig

Description

Validation rule:

  • not empty
  • numeric only
  • allow "XXXXX", "XXXXX XXXX", or "XXXXX-XXXX"
  • first 5 digits cannot be "00000" (so "00000", "00000 XXXX" and "00000-XXXX" will be regected)
Submitted by Zheyang Song - 8 years ago