The Luhn algorithm (Wikipedia), developed in the 1950’s by IBM scientist Hans Peter Luhn, is a “simple checksum formula used to validate a variety of identification numbers.” Such numbers range from credit card numbers to various types of government identification numbers such as NPI numbers issued to health care providers.
Using the Laravel Luhn package, you can validate numbers using the package’s validation rule:
<?php $validator = Validator::make($data, [ 'number1' => 'luhn', // Using shorthand notation 'number2' => new LuhnRule(), // Using custom rule class ]);
The Luhn algorithm implementation using the provided facade includes three methods, including
<?php Luhn::isValid('1234'); Luhn::computeCheckDigit('1234'); Luhn::computeCheckSum('1234');
Check out marvinlabs/laravel-luhn on GitHub for complete installation and usage instructions. If you are interested in learning more about the Luhn algorithm, the Luhn algorithm page on Wikipedia has some good information, even including some code examples of how the algorithm works.
Filed in: Laravel Packages
Join the weekly newsletter and never miss out on new tips, tutorials, and more.
- PHP Web Developer
Christ for all Nations
- PHP Developer
Manhattan, KS (open to Remote)
- Mid-Senior Laravel Developer
- Full Stack Web Developer
Las Vegas, NV
- Staff Software Engineer (Laravel)
- Software Engineer (PHP, Laravel, Vue)
Los Angeles, CA
- Software Engineer Level 1 / Level 2
St. Pete. / Tampa Area, FL / Remote
PHP 7.3’s First Release Candidate is Here
PHP 7.3 is inching closer to general release (planned for December), with today’s announcement of the first PHP 7.3 r…
Laratables: Ajax support of DataTables in Laravel
Laratables is a package by Gaurav Makhecha (https://github.com/gauravmak) to handle server-side AJAX of DataTables in…