Recent Posts

Sponsors
![]() |
Validate a Credit CardDawn Rossi, 12-10-2008 |
This little function helps validating a given credit card number is legit:
function ValidateCreditCardNumber($cc_num)
{
$pattern = "/^3[47]\d{13}$/";//American Express
if (preg_match($pattern,$cc_num))
{
return true;
}
$pattern = "/^([30|36|38]{2})([0-9]{12})$/";//Diner's Club
if (preg_match($pattern,$cc_num))
{
return true;
}
$pattern = "/^6011\d{12}$/";//Discover Card
if (preg_match($pattern,$cc_num))
{
return true;
}
$pattern = "/^5[12345]\d{14}$/";//Mastercard
if (preg_match($pattern,$cc_num))
{
return true;
}
$pattern = "/^4\d{12}(\d\d\d){0,1}$/";//Visa
if (preg_match($pattern,$cc_num))
{
return true;
}
$pattern = "/^30[012345]\d{11}$/";//Diners
if (preg_match($pattern,$cc_num))
{
return true;
}
$pattern = "/^3[68]\d{12}$/";//Diners #2
if (preg_match($pattern,$cc_num))
{
return true;
}
// Not valid
return false;
}
function ValidateCreditCardNumber($cc_num)
{
$pattern = "/^3[47]\d{13}$/";//American Express
if (preg_match($pattern,$cc_num))
{
return true;
}
$pattern = "/^([30|36|38]{2})([0-9]{12})$/";//Diner's Club
if (preg_match($pattern,$cc_num))
{
return true;
}
$pattern = "/^6011\d{12}$/";//Discover Card
if (preg_match($pattern,$cc_num))
{
return true;
}
$pattern = "/^5[12345]\d{14}$/";//Mastercard
if (preg_match($pattern,$cc_num))
{
return true;
}
$pattern = "/^4\d{12}(\d\d\d){0,1}$/";//Visa
if (preg_match($pattern,$cc_num))
{
return true;
}
$pattern = "/^30[012345]\d{11}$/";//Diners
if (preg_match($pattern,$cc_num))
{
return true;
}
$pattern = "/^3[68]\d{12}$/";//Diners #2
if (preg_match($pattern,$cc_num))
{
return true;
}
// Not valid
return false;
}
![]() |
seosoeasy, 12-12-2008 |
Ohh cool job! By last week i read an article relating how to reduce Credit Card Fraud for Internet Merchants.It was interesting.But now i have some clear idea by your excellent posts.I really appreciate your hard work.You are rocking.Thanks.
![]() |
Geoserv, 01-13-2009 |
STUMBLED!
Wow, very nice piece of code.
Thanks for posting.
Wow, very nice piece of code.
Thanks for posting.
![]() |
That Guy, 01-17-2009 |
I stumbled this!
I don't really know what its written in. Is it Javascript? n ot perl.
I don't really know what its written in. Is it Javascript? n ot perl.
![]() |
john, 01-17-2009 |
@That Guy, Today: php
![]() |
kamikaze.cockroach, 01-19-2009 |
@That Guy: Looks like PHP to me.
![]() |
David, 01-20-2009 |
Looks good.
One note: as it will stop processing as soon as return is called, I'd move the check for Visa to the top, and maybe follow with MasterCard, while putting the less frequent ones, like Diner's Club, near the bottom so that they only get executed if it is not a more common card.
Also, as the code is the same every time, you could save yourself a few lines by just putting the pattern strings in an array and iterating through.
One note: as it will stop processing as soon as return is called, I'd move the check for Visa to the top, and maybe follow with MasterCard, while putting the less frequent ones, like Diner's Club, near the bottom so that they only get executed if it is not a more common card.
Also, as the code is the same every time, you could save yourself a few lines by just putting the pattern strings in an array and iterating through.
![]() |
chris, 01-21-2009 |
@David: Unless you're going to be processing a large number of cards, the order of the checks remains negligible.
The array idea is a good one, but I think that for the purposes of illustrating card verification this code serves it's purposes better.
I might also return which type of card validated (i.e. this is a visa that correctly passed validation) as well but that would be just me :)
The array idea is a good one, but I think that for the purposes of illustrating card verification this code serves it's purposes better.
I might also return which type of card validated (i.e. this is a visa that correctly passed validation) as well but that would be just me :)
![]() |
mohrt, 01-24-2009 |
Just to clarify, an luhn-10 check is only a syntax check, it doesn't check whether or not the card number is actually a valid card belonging to someone. If a card number is typed in and even one number is out of place, the luhn-10 check will fail.
![]() |
Richard, 04-15-2009 |
This is PHP, but the REG EXP can be used in almost any language :P
![]() |
Robin Nixon, 09-11-2010 |
Here's a more recent script that covers more cards and has the Luhn algorithm:
http://www.pluginphp.com/plug-in32.php
http://www.pluginphp.com/plug-in32.php
![]() |
aceph, 05-23-2011 |
Great Job, it seems like this only function might work on all kinds of credit cards :)
![]() |
Mr inZen, 10-07-2012 |
THanx for the great post and helpful replies! Keep on Rocking!!
![]() |
Donne Roma, 01-25-2013 |
Very important information. Thanks for sharing
![]() |
Easy Commerce, 03-20-2013 |
Such a useful coding information. Thanks for sharing with clear code structure.
![]() |
personal vpn, 12-04-2013 |
You have brought up very fantastic points, thank you for the post.
![]() |
Kyle Johnson, 05-22-2014 |
Thank you I've already put the code to use.
![]() |
Hire Php Developer, 01-21-2017 |
After a searching a lot finally found the code. Thanks a lot.
|

Subscribe Now to receive new posts via Email as soon as they come out.
Comments
Post your comments