axlon / laravel-postal-code-validation
Worldwide postal code validation for Laravel
Package info
github.com/axlon/laravel-postal-code-validation
pkg:composer/axlon/laravel-postal-code-validation
Requires
- php: ^7.2 || ^8.0
- illuminate/contracts: ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0
- illuminate/support: ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0
- illuminate/validation: ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0
Requires (Dev)
- guzzlehttp/guzzle: ^7.8
- phpunit/phpunit: ^8.5 || ^9.6 || ^10.5
- symfony/var-exporter: ^4.0 || ^5.0 || ^6.0 || ^7.0
This package is auto-updated.
Last update: 2026-08-30 21:03:32 UTC
README
Adds postal code validation to Laravel, based on Google's libaddressinput.
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