PostalCode

February 3, 2026 · View on GitHub

  • PostalCode(string $countryCode)
  • PostalCode(string $countryCode, bool $formatted)

Validates whether the input is a valid postal code or not.

v::postalCode('BR')->assert('02179000');
// Validation passes successfully

v::postalCode('BR')->assert('02179-000');
// Validation passes successfully

v::postalCode('US')->assert('02179-000');
// → "02179-000" must be a postal code for "US"

v::postalCode('US')->assert('55372');
// Validation passes successfully

v::postalCode('PL')->assert('99-300');
// Validation passes successfully

By default, PostalCode won't validate the format (puncts, spaces), unless you pass $formatted = true:

v::postalCode('BR', true)->assert('02179000');
// → "02179000" must be a postal code for "BR"

v::postalCode('BR', true)->assert('02179-000');
// Validation passes successfully

Message template for this validator includes {{countryCode}}.

Extracted from GeoNames.

Templates

PostalCode::TEMPLATE_STANDARD

ModeTemplate
default{{subject}} must be a postal code for {{countryCode}}
inverted{{subject}} must not be a postal code for {{countryCode}}

Template placeholders

PlaceholderDescription
countryCode
subjectThe validated input or the custom validator name (if specified).

Categorization

  • Localization
  • Strings

Changelog

VersionDescription
3.0.0Templates changed
2.3.0Add option to validate formatting
2.2.4Cambodian postal codes now support 5 and 6 digits
0.7.0Created

See Also