axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel

Maintainers

Package info

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

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

Transparency log

Statistics

Installs: 3 810 928

Dependents: 1

Suggesters: 0

Stars: 389

Open Issues: 1

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. Arguments must be country codes in ISO 3166-1 alpha-2 format.

use Axlon\PostalCodeValidation\Rules\PostalCode;

request()->validate([
    'postal_code' => PostalCode::of('NL', 'BE'),
]);

Sometimes, you may not know the area you are validating against before the request. To solve this you may pass the name of the field holding the area code instead.

use Axlon\PostalCodeValidation\Rules\PostalCode;

request()->validate([
    'country' => 'required|in:NL,BE',
    'postal_code' => [
        'required', PostalCode::of('country'),
    ],
]);

Alternatively, if you prefer defining validation rules as strings you may use the postal_code rule:

request()->validate([
    'shipping_address.postal_code' => 'postal_code:NL',
]);

Adding an error message

To add a meaningful error message, add the following lines to lang/{language}/validation.php:

'postal_code' => 'Your message here',

The following placeholders will be automatically filled for you:

Placeholder Description
:attribute The name of the field that was under validation
:areas The areas that were validated against (e.g. NL, BE)
:examples Examples of allowed postal codes (e.g. 1234 AB, 4000)

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 converted into PHP resource files