Laravel-Validate Financial Validation Rules: VAT, IBAN, Credit Cards & Crypto
The milwad-dev/laravel-validate package provides five specialized financial validation rules—ValidVatId, ValidIban, ValidCreditCard, ValidCartNumberIran, and ValidBitcoinAddress—that validate European tax IDs, international bank accounts, payment cards, and cryptocurrency addresses using industry-standard algorithms.
The milwad-dev/laravel-validate package extends Laravel’s validator with production-ready financial validation rules that handle complex checksum algorithms and format specifications. These rules implement the Illuminate\Contracts\Validation\Rule contract and are automatically registered through the package’s service provider, allowing you to validate sensitive financial identifiers without writing custom regex or mathematical validation logic.
Available Financial Validation Rules
The package ships with five dedicated rules covering common financial identifiers used in web applications.
ValidVatId – European VAT Identification Numbers
The ValidVatId rule validates European Union Value Added Tax identification numbers. Located in src/Rules/ValidVatId.php, this rule strips non-alphanumeric characters and enforces the format ^[A-Z]{2}[0-9]{0,12}$, ensuring the value starts with a two-letter country code followed by up to 12 digits.
ValidIban – International Bank Account Numbers
The ValidIban rule in src/Rules/ValidIban.php validates IBANs using the IbanTrait found in src/Traits/IbanTrait.php. The validation process converts the IBAN to numeric form, verifies the length against country-specific requirements, and performs the MOD-97 checksum to detect transcription errors.
ValidCreditCard – Credit Card Numbers
Located in src/Rules/ValidCreditCard.php, this rule implements the Luhn algorithm (modulus 10 check) to validate credit card numbers from any major brand. The rule automatically removes non-digit characters before running the checksum calculation.
ValidCartNumberIran – Iranian Payment Cards
The ValidCartNumberIran rule in src/Rules/ValidCartNumberIran.php validates 16-digit Iranian payment card numbers using a country-specific Luhn-style checksum algorithm tailored to Iran’s banking format.
ValidBitcoinAddress – Cryptocurrency Wallets
The ValidBitcoinAddress rule in src/Rules/ValidBitcoinAddress.php validates Bitcoin wallet addresses using regular expressions that match both legacy Base58 addresses and modern Bech32 (SegWit) formats.
How to Use Financial Validation Rules in Laravel
You can instantiate these rules as objects or use their registered string aliases in validation arrays.
Object-Based Validation
Import the rule classes directly and instantiate them in your validation logic:
use Milwad\LaravelValidate\Rules\ValidVatId;
use Milwad\LaravelValidate\Rules\ValidIban;
use Milwad\LaravelValidate\Rules\ValidCreditCard;
use Milwad\LaravelValidate\Rules\ValidCartNumberIran;
use Milwad\LaravelValidate\Rules\ValidBitcoinAddress;
$validator = validator($request->all(), [
'vat_id' => ['required', new ValidVatId()], // e.g., "DE123456789"
'iban' => ['required', new ValidIban()], // e.g., "DE89370400440532013000"
'card_number' => ['required', new ValidCreditCard()], // Any 16-digit card
'iran_card' => ['required', new ValidCartNumberIran()], // Iranian 16-digit card
'btc_wallet' => ['required', new ValidBitcoinAddress()], // Bitcoin address
]);
String Alias Validation
The LaravelValidateServiceProvider registers each rule via Validator::extend(), making them available in Laravel’s pipe syntax:
$rules = [
'vat_id' => 'required|valid_vat_id',
'iban' => 'required|valid_iban',
'card_number' => 'required|valid_credit_card',
'iran_card' => 'required|valid_cart_number_iran',
'btc_wallet' => 'required|valid_bitcoin_address',
];
Both approaches invoke the same underlying logic; the string alias method is preferable when you need to store validation rules in configuration files or database columns.
Architecture and Implementation Details
Understanding how these rules integrate with Laravel helps you extend them for custom financial instruments.
Rule Contract Implementation
Each financial rule implements the Illuminate\Contracts\Validation\Rule interface with two required methods:
passes($attribute, $value): bool– Contains the core validation logic (regex checks, checksum calculations, or algorithm implementations).message(): string– Returns the translation keyvalidate.<rule-name>(e.g.,validate.valid_vat_id), which Laravel resolves from the localization files.
Service Provider Registration
The src/LaravelValidateServiceProvider.php file automatically registers all custom validators during the boot process. This registration maps string aliases like valid_iban to their respective rule classes, enabling seamless integration with Laravel’s Validator facade.
Localization Support
Error messages for all financial rules are stored in resources/lang/*/validate.php. The package ships with default English translations, but you can override them or add new locale files to provide region-specific validation feedback for financial data.
Reusable Traits
The ValidIban rule leverages the IbanTrait to isolate the MOD-97 checksum logic. This architectural decision keeps the rule class focused on validation orchestration while the trait handles complex mathematical operations, making the code maintainable and reusable for future IBAN-related components.
Summary
- Five specialized rules cover VAT IDs, IBANs, credit cards, Iranian payment cards, and Bitcoin addresses.
- Algorithmic validation includes MOD-97 checksums for IBANs and Luhn algorithms for credit cards.
- File locations: Rules reside in
src/Rules/(e.g.,ValidVatId.php,ValidIban.php), with shared logic insrc/Traits/IbanTrait.php. - Dual usage patterns: Instantiate rule objects or use string aliases registered by
LaravelValidateServiceProvider. - Localization ready: Error messages use the
validate.<rule-name>convention and are stored inresources/lang/*/validate.php.
Frequently Asked Questions
How do I validate a European VAT ID in Laravel?
Use the ValidVatId rule from milwad-dev/laravel-validate. You can either instantiate new ValidVatId() in your validation array or use the valid_vat_id string alias. The rule validates the format ^[A-Z]{2}[0-9]{0,12}$ after stripping non-alphanumeric characters.
What algorithm does the IBAN validation use?
The ValidIban rule uses the MOD-97 checksum algorithm as defined in ISO 13616. Implemented in src/Traits/IbanTrait.php, the validation converts the IBAN to a numeric representation, checks country-specific length requirements, and verifies the checksum to ensure the bank account number is mathematically valid.
Does the package support Bitcoin address validation?
Yes. The ValidBitcoinAddress rule in src/Rules/ValidBitcoinAddress.php validates both legacy Base58 and Bech32 Bitcoin addresses using regular expressions that check character sets and address length constraints.
How are error messages handled for financial validation failures?
Each rule implements the message() method to return a translation key in the format validate.<rule-name>. Laravel resolves these keys from resources/lang/*/validate.php, allowing you to customize error messages for each financial validation scenario in any supported language.
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 →