Country-Specific Validators in Laravel Validate: Landlines, IDs, and Postal Codes
The milwad-dev/laravel-validate package provides country-specific validators for landline numbers, Iranian national identification numbers, postal codes, IBAN formats, and ISO-3166-1 country codes through a centralized callback architecture.
The milwad-dev/laravel-validate package extends Laravel's validation capabilities with specialized rules for international data formats. While phone number validation is commonly documented, the library offers several other country-specific validators that handle landline formats, government-issued IDs, and geographic codes. These validators follow a consistent pattern using callback registries and dedicated validator classes under src/Utils/.
Landline Number Validators
The package includes dedicated landline validators for 16 countries, each implementing the CountryLandlineValidator interface. Located in src/Utils/CountryLandlineValidator/, these classes validate domestic landline formats using country-specific regex patterns.
Available Landline Validators
The following concrete validator classes are registered in src/Utils/CountryLandlineCallback.php:
- ZHLandlineValidator: China (ZH)
- SELandlineValidator: Sweden (SE)
- TRLandlineValidator: Turkey (TR)
- SALandlineValidator: Saudi Arabia (SA)
- RULandlineValidator: Russia (RU)
- KOLandlineValidator: South Korea (KO)
- JALandlineValidator: Japan (JA)
- ITLandlineValidator: Italy (IT)
- IRLandlineValidator: Iran (IR)
- INLandlineValidator: India (IN)
- IDLandlineValidator: Indonesia (ID)
- GRLandlineValidator: Greece (GR)
- FRLandlineValidator: France (FR)
- ESLandlineValidator: Spain (ES)
- ENLandlineValidator: England (EN)
- DELandlineValidator: Germany (DE)
- CMLandlineValidator: Cameroon (CM)
How Landline Validation Works
In src/Utils/CountryLandlineCallback.php, a central registry maps ISO-alpha-2 country codes to concrete validator classes. The ValidLandlineNumber rule in src/Rules/ValidLandlineNumber.php delegates validation to this callback, which resolves the appropriate class and executes its validate(string $value): bool method. If the resolved class does not implement CountryLandlineValidator, the system throws a runtime exception.
Iran-Specific Government ID Validators
Beyond telecommunications, the package provides three specialized validators for Iranian administrative data. These rules are hardcoded for Iranian formats and reside directly in src/Rules/.
ValidNationalCard
The ValidNationalCard rule validates Iranian National ID numbers (Melli Code) using the official checksum algorithm implemented in the class.
ValidIranPostalCode
The ValidIranPostalCode rule verifies Iranian postal code formats, ensuring the value matches the country's specific addressing standards.
ValidCartNumberIran
The ValidCartNumberIran rule validates Iranian "cart" numbers, applying format checks distinct from the national ID system for specific domestic identification cards.
Geographic and Financial Code Validation
The library includes two additional country-aware validators that leverage the central Country utility class.
ValidCountry for ISO Codes
The ValidCountry rule in src/Rules/ValidCountry.php validates whether a string represents a legitimate ISO-3166-1 alpha-2 country code. This rule references the comprehensive country list defined in src/Utils/Country.php.
ValidIban with Country-Specific Lengths
While ValidIban validates the International Bank Account Number format, it uses the ibanLengthByCountry map defined in src/Utils/Country.php to enforce correct IBAN lengths for over 150 countries. For example, German IBANs must be exactly 22 characters as specified by Country::DEUTSCHLAND => 22.
Implementation Architecture
The service provider src/LaravelValidateServiceProvider.php registers all country-specific validators during the boot cycle using:
CountryLandlineCallback::addValidator($code, $country);
This registration system allows the validation rules to resolve the correct country-specific implementation at runtime without hardcoding validator logic in the rules themselves.
Practical Usage Examples
use Milwad\LaravelValidate\Rules\ValidLandlineNumber;
use Milwad\LaravelValidate\Rules\ValidNationalCard;
use Milwad\LaravelValidate\Rules\ValidCountry;
use Milwad\LaravelValidate\Rules\ValidIban;
// Validate a German landline number
$request->validate([
'landline' => ['required', new ValidLandlineNumber('DE')],
]);
// Validate an Iranian National ID
$request->validate([
'national_id' => ['required', new ValidNationalCard()],
]);
// Verify ISO-3166-1 alpha-2 country code
$request->validate([
'country_code' => ['required', new ValidCountry()],
]);
// Validate IBAN with country-specific length checking
$request->validate([
'bank_account' => ['required', new ValidIban()],
]);
Summary
- The package provides 16 country-specific landline validators located in
src/Utils/CountryLandlineValidator/for nations including Germany, Japan, India, and Russia. - Iranian administrative data is covered through
ValidNationalCard,ValidIranPostalCode, andValidCartNumberIranrules. - ISO-3166-1 country codes are validated via
ValidCountryusing the centralCountryutility class. - IBAN validation enforces country-specific length requirements using the
ibanLengthByCountrymap insrc/Utils/Country.php. - All validators follow a callback-based architecture registered in
src/LaravelValidateServiceProvider.php, separating validation logic from Laravel rule definitions.
Frequently Asked Questions
Which countries are supported for landline validation besides phone numbers?
The package supports landline validation for 16 countries: China (ZH), Sweden (SE), Turkey (TR), Saudi Arabia (SA), Russia (RU), South Korea (KO), Japan (JA), Italy (IT), Iran (IR), India (IN), Indonesia (ID), Greece (GR), France (FR), Spain (ES), England (EN), Germany (DE), and Cameroon (CM). Each has a dedicated validator class in src/Utils/CountryLandlineValidator/ that is resolved through the CountryLandlineCallback registry.
How do I validate an Iranian National ID number?
Use the ValidNationalCard rule from Milwad\LaravelValidate\Rules\ValidNationalCard. This rule implements the official Iranian National ID (Melli Code) checksum algorithm and does not require a country code parameter as it is specifically designed for Iranian formats.
Can I validate any country code format, or only specific countries?
The ValidCountry rule validates any string against the ISO-3166-1 alpha-2 standard using the comprehensive country list in src/Utils/Country.php. This accepts over 150 country codes, unlike the landline validators which only support the 16 specifically implemented nations.
Does the IBAN validator support all countries?
Yes, the ValidIban rule supports IBAN validation for over 150 countries by referencing the ibanLengthByCountry map in src/Utils/Country.php. It validates both the format and the country-specific length requirements, such as 22 characters for Germany and 28 characters for Pakistan.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →