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
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-20 21:49:31 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.
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