Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

laravel-postal-code-validation

> 后端框架
Open source

Worldwide postal code validation for Laravel and Lumen

389 stars0 likes1 views
WebsiteGitHub

About

Worldwide postal code validation for Laravel and Lumen

Laravel Postal Code Validation

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

  • Requirements
  • Installation
  • Usage
    • Available rules
    • Fluent API
    • Adding an error message
  • License
  • Attribution

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:

bash
composer require axlon/laravel-postal-code-validation

If you have package discovery enabled, that's it, continue to the usage section. If you want to register the package manually, you can do this by adding the following line to your config/app.php file:

php
'providers' => [
   ...
   Axlon\PostalCodeValidation\ValidationServiceProvider::class,
   ...
],

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:foo,bar,...

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

php
'postal_code' => 'postal_code:NL,DE,FR,BE'

postal_code_with:foo,bar,...

The field under validation must be a postal code in at least one of the countries in the given fields only if at least one of the specified fields is present.

php
'billing.country' => 'required|string|max:2',
...
'shipping.country' => 'nullable|string|max:2',
'shipping.postal_code' => 'postal_code_with:billing.country,shipping.country'

Fluent API

If you prefer using a fluent object style over string based rules, that's also available:

php
'postal_code' => [
    PostalCode::for('NL')->or('BE'),
],

The same goes for the postal_code_with rule:

php
'billing.country' => 'required|string|max:2',
...
'shipping.country' => 'nullable|string|max:2',
'shipping.postal_code' => [
    PostalCode::with('billing.country')->or('shipping.country')
],

Adding an error message

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

php
'postal_code' => 'Your message here',
'postal_code_with' => 'Your message here',

The following placeholders will be automatically filled for you:

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

*The :countries and :examples placeholders may be empty if no valid countries are passed.

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

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPlaravellaravel-packagelumenpostal-code

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架