Search by

axlon / laravel-postal-code-validation

axlon

Worldwide postal code validation for Laravel

Package info

github.com/axlon/laravel-postal-code-validation

pkg:composer/axlon/laravel-postal-code-validation

Statistics

Installs: 3 896 495

Dependents: 1

Suggesters: 0

Stars: 390

Open Issues: 0

v3.10.0 2026-08-09 20:25 UTC

README

Adds postal code validation to Laravel, based on Google's libaddressinput.

Codecov Downloads Latest version License

Requirements

This package has the following requirements:

  • PHP 8.2 or greater
  • Laravel 12 or greater

Installation

You can install this package with Composer, by running the command below:

composer require axlon/laravel-postal-code-validation

Usage

Postal code validation perfectly integrates into your Laravel application, you can use it just like you would any framework validation rule.

Available rules

This package adds the following validation rules:

postal_code

The field under validation must be a valid postal code in at least one of the given regions. Each argument may be either an uppercase ISO 3166-1 alpha-2 country code or the name of a field that contains one. Region codes are case-sensitive.

'postal_code' => 'postal_code:NL,BE'

Fields can be referenced using dot notation:

'shipping.country' => 'nullable|string|size:2|uppercase',
'shipping.postal_code' => 'postal_code:shipping.country'

Rule object

This package also provides a PostalCode rule object, which allows for fluent rule building:

'postal_code' => [
    PostalCode::of(['NL', 'BE']),
],

It accepts the same arguments as the postal_code rule does.

Default configuration

If you use the same configuration in multiple places, you may want to set a default way to validate postal codes. Typically, this should be done in a service provider's boot() method:

use Axlon\PostalCodeValidation\Rules\PostalCode;

PostalCode::defaults(static fn () => PostalCode::of('NL'));

You can then easily invoke this default configuration by calling PostalCode::default(), or by calling the postal_code rule without any arguments.

Adding an error message

Add the following line to lang/{locale}/validation.php:

'postal_code' => 'Your message here.',

The following placeholders are automatically filled for you:

Placeholder Description
:attribute The name of the field that was under validation
:regions The regions that were validated against (e.g. NL, BE)

Important

The :regions placeholder will be empty when referenced fields do not contain any country codes.

License

This software is licenced under the MIT license.

Attribution

This software contains data derived from Google's Address Validation Metadata. The original data is provided by Google and is licensed under the CC-BY 4.0 license.

Modifications

  • Only data relevant to postal code validation is included
  • Regular expressions found in the data are adjusted to be compatible with PHP's regex engine
  • The resulting data is used to generate PHP files