/ Published in: PHP
Validate the credit card number is valid or not
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php 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 $verified = true; } else { $verified = false; } } elseif($type == "Dinners") { $pattern = "/^([30|36|38]{2})([0-9]{12})$/";//Diner's Club $verified = true; } else { $verified = false; } } elseif($type == "Discover") { $pattern = "/^([6011]{4})([0-9]{12})$/";//Discover Card $verified = true; } else { $verified = false; } } elseif($type == "Master") { $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/";//Mastercard $verified = true; } else { $verified = false; } } elseif($type == "Visa") { $pattern = "/^([4]{1})([0-9]{12,15})$/";//Visa $verified = true; } else { $verified = false; } } if($verified == false) { //Do something here in case the validation fails echo "Credit card invalid. Please make sure that you entered a valid <em>" . $denum . "</em> credit card "; } else { //if it will pass...do something echo "Your <em>" . $denum . "</em> credit card is valid"; } } echo validateCC("4111111111111111", "Visa"); ?>
URL: http://www.roscripts.com/How_to_validate_credit_card_numbers-106.html