» Web Development » Validate Credit Card Number Using PHP

Validate Credit Card Number Using PHP

Credit card processing is the most crucial factor when it comes to online and e commerce websites. Having abilities to charge someone’s credit card give you a whole new set of responsibilities. In a nutshell you may responsible for each and every transaction or transaction attempt which customers make. Hence it would be wise to secure your system little more and block invalid payment attempts. In this post we have mentioned How to Validate Credit Card Number Using PHP. If your e commerce website has made with PHP, this tutorial might help you.

There are many active credit card types available such as MASTERCARD, VISA, AMEX and Discover. You need to validate no matter what the credit card type provided by users in order to have a robust and secured credit card processing system. Validate Credit Card Number Using PHP can be separated in to three steps.

  1. Credit Card Number Pattern Validation
  2. Credit Card Number Checksum Validation
  3. Credit Card Status Validation

In the credit card number pattern validation process, the main concern is crosscheck the given number against the pattern of available credit card types. These patterns are vary for each card type. For an example,

  • Marstercard numbers always start with 51,52, 53, 54 or 55. Number length is always considered as 16.
  • Visa cards have 4 as their prefix while the the length is 13 or 16.
  • Amex credit card numbers has 15 numbers and they start with 34 or 37.

The most important step in Validate Credit Card Number Using PHP is the checksum validation. A checksum defined as a small datum which allows to detect errors of data or validate data integrity. In PHP we can use mod10 algorithm AKA Luhn algorithm to validate Credit card number using PHP.

This algorithm was invented by IBM scientist Peter Luhn in mid 90s. Luhn Function can be used to validate variety of identification numbers including credit card numbers.

In order to validate whether a particular credit is active or inactive you need a merchant account from the payment gateway supplier which you should already have.

So, Let’s get in to work. Following PHP function will check whether the provided number does match with every major credit card types.

function getCCType($cardNumber) {
// Remove non-digits from the number
$cardNumber = preg_replace('/\D/', '', $cardNumber);

// Validate the length
$len = strlen($cardNumber);
if ($len < 15 || $len > 16) {
throw new Exception("Invalid credit card number. Length does not match");
switch($cardNumber) {
case(preg_match ('/^4/', $cardNumber) >= 1):
return 'Visa';
case(preg_match ('/^5[1-5]/', $cardNumber) >= 1):
return 'Mastercard';
case(preg_match ('/^3[47]/', $cardNumber) >= 1):
return 'Amex';
case(preg_match ('/^3(?:0[0-5]|[68])/', $cardNumber) >= 1):
return 'Diners Club';
case(preg_match ('/^6(?:011|5)/', $cardNumber) >= 1):
return 'Discover';
case(preg_match ('/^(?:2131|1800|35\d{3})/', $cardNumber) >= 1):
return 'JCB';
throw new Exception("Could not determine the credit card type.");

Once a credit card number passed above function and returned with a known credit card type, the next step would be to validate it against the checksum. As mentioned above we are using Luhn algorithm for the purpose.

function validateChecksum($number) {

// Remove non-digits from the number
$number=preg_replace('/\D/', '', $number);

// Get the string length and parity
$number_length = strlen($number);
$parity = $number_length % 2;

// Split up the number into single digits and get the total
for ($i=0; $i<$number_length; $i++) {
// Multiply alternate digits by two
if ($i % 2 == $parity) {
// If the sum is two digits, add them together
if ($digit > 9) {
// Sum up the digits

// If the total mod 10 equals 0, the number is valid
return ($total % 10 == 0) ? TRUE : FALSE;


However it is up to you to decide whether you need to validate the credit card number prior to submit to the payment gateway, Besides payment gateway will always validate credit card numbers in every possible ways. You can read more about Luhn algorithm in this wiki article. Here is a list of all bank cards.

Lakshan Jayasinghe

Lakshan is a passionate blogger and a geek! He is graduated from the University of Colombo, School of Computing. He is currently working as a Software Engineer in Colombo, with a passion for technology, coding & every kind of gadgetry.

Add comment

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Most discussed