New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel
4,332,490 391
Install
composer require axlon/laravel-postal-code-validation
Latest Version:v3.11.1
PHP:^7.2 || ^8.0
Laravel:5 – 13
License:MIT
Last Updated:Nov 26, 2026
Links: GitHub  ·  Packagist
Maintainer: axlon

[!WARNING] This documentation is for version 4.x, which is still in development. For production, use version 3.x and follow the 3.x documentation.

Laravel Postal Code Validation

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

Tests Downloads Latest version License

Requirements

This package has the following requirements:

  • Laravel 12 or greater
  • PHP 8.2 or greater

Installation

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

composer require axlon/laravel-postal-code-validation

Laravel registers the service provider through package discovery. To register it manually, add it to your Laravel application's bootstrap/providers.php file:

return [
    // ...
    Axlon\PostalCodeValidation\PostalCodeServiceProvider::class,
];

Usage

Validate a postal code

The field under validation must be a valid postal code in at least one of the specified areas. Arguments must be countries in ISO 3166-1 alpha-2 format:

use Axlon\PostalCodeValidation\Rules\PostalCode;

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

Validate a postal code against a dynamic area

You may also validate a postal code against an area code that is passed in the request:

use Axlon\PostalCodeValidation\Rules\PostalCode;

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

String notation

Use postal_code with one or more comma-separated parameters. Just like with the object rule, you can mix literal area codes and input field references:

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

Adding an error message

To add a meaningful error message, add an error message to resources/lang/{your language}/validation.php:

'postal_code' => 'The :attribute must be a valid postal code for :areas.',

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)

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

Related Packages

spikkl/spikkl-php-laravel-client

Spikkl API client wrapper for Laravel

1,072 0
claraleigh/naughty-username

Validate that a username is not a naughty one

24,812 0
cohensive/validation

Extra functionality for Laravel 5 Validator.

13,938 14

Version History

Version Released PHP Laravel License
v3.11.1 ^7.2 || ^8.0 ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0 MIT
v3.11.0 ^7.2 || ^8.0 ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0 MIT
v3.10.0 ^7.2 || ^8.0 ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0 MIT
v3.9.1 ^7.2 || ^8.0 ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0 MIT
v3.9.0 ^7.2 || ^8.0 ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0 MIT