In this article i have explain How to validate the credit card using php this funtion This function will validate a credit card is valid or not.
Bellow is the list of rules for top six most popular credit cards :
PHP Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | <?php /** * PHP function to Validates the credit card. *@author : Prem Tiwari */ error_reporting('1'); function validateCC($cc_num, $type) { if($type == "American") { $denum = "American Express"; } elseif($type == "Dinners") { $denum = "Diner's Club"; } elseif($type == "Discover") { $denum = "Discover"; } elseif($type == "Master") { $denum = "Master Card"; } elseif($type == "Visa") { $denum = "Visa"; } if($type == "American") { $pattern = "/^([34|37]{2})([0-9]{13})$/";//American Express if (preg_match($pattern,$cc_num)) { $verified = true; } else { $verified = false; } } elseif($type == "Dinners") { $pattern = "/^([30|36|38]{2})([0-9]{12})$/";//Diner's Club if (preg_match($pattern,$cc_num)) { $verified = true; } else { $verified = false; } } elseif($type == "Discover") { $pattern = "/^([6011]{4})([0-9]{12})$/";//Discover Card if (preg_match($pattern,$cc_num)) { $verified = true; } else { $verified = false; } } elseif($type == "Master") { $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/";//Mastercard if (preg_match($pattern,$cc_num)) { $verified = true; } else { $verified = false; } } elseif($type == "Visa") { $pattern = "/^([4]{1})([0-9]{12,15})$/";//Visa if (preg_match($pattern,$cc_num)) { $verified = true; } else { $verified = false; } } } if(isset($_POST['submit'])) { if($verified == false) { //Do something here in case the validation fails $msg = "<span class='alert-danger'>Credit card invalid. Please make sure that you entered a valid <em>" . $_POST['cccard'] . "</em> credit card </span>"; } else { //if it will pass...do something $msg = "<span class='alert-success'>Your <em>" . $_POST['cccard'] . "</em> credit card is valid</span>"; } //echo validateCC("4111111111111111", "Visa"); echo validateCC($_POST['cccard'], $_POST['cctype']); } ?> |
You can use this libraryfunction to validate the credit card. This is free for Demo and code download. Please see the demo first and if you like it then download it.
I hope you like this simple and easy PHP function if yes then please feel free to comment below.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.