Recent Articles

Credit Card validation regex script in PHP using Luhn algorithm

Credit card validation is essential for e-commerce websites. Credit card numbers entered by users will not be necessarily correct always. The credit card payment processing companies (visa,mastercard) charge merchandise for each transaction carried out. This also includes the failed transactions. Therefore it becomes essential to validate the credit card number  before sending it to the payment gateways. The unique credit card numbers are generated by the issuing banks using Luhn formula. This tutorial explains how to validate the credit card number entered by user and get the name of the payment network (visa, mastercard etc..) of the credit card. There are different formats for the credit card numbers allotted by the payment networks. The various payment networks and their credit card number formats are given below.

  • Visa : 16 digit for new and 13 digit number for old cards. Number starts with 4
  • Mastercard : 16 digit number starting with 51 through 55
  • America Express (Amex) : 15 digit number starting with 34 or 37
  • Maestro : 12, 16 or 19 digit number with prefixes, including 50, 56-58, 67,6304 or 6390.
  • Discover : 16 digit number beginning with 6011 or 65
  • JCB : 16 digit number starting with 35 or 15 digit number starting with 2131,1800

Luhn algorithm is used to generate credit card numbers by the banks. This formula is only used for distinguishing specific numbers from random numbers. This eliminates the accidental errors by users while providing the credit card number. To validate credit card number; we will proceed with two steps.

 

1. Check if the credit card number is valid using Luhn algorithm

The following function returns whether the credit card number is valid or not using Luhn formula.

The validateCard() function used in above code is described in step 2 below.

 

2. Identify the payment network using regular expression

If the card number entered by user is a valid credit card, then the payment network authority can be identified by using the following function. The function takes the valid credit card number and checks it against the possible payment networks using regular expression. The regex are mentioned in the $cards array as values for the payment network keys.

 

Final Code

Both above mentioned functions can be combined to check the valid credit card number using regular expression (regex) and identifying the card’s payment network name.

The Credit card number is supplied to ‘is_valid_card()’ function as an argument. If the card is valid, then validateCard() function is internally called which returns the name of the payment network.
The function call is given below.

$card = “371449635398431″;
echo is_valid_card($card);

Note : It is not advisable to publish credit card numbers while demonstrating payment applications. Sample credit card numbers for testing can be taken from Auricsystems.

  • thohan

    I think the Maestro regex is supposed to be (?:5020|5038|6304|6579|6761|6799)\d{12}(?:ddd)

    I just added another d to last section. I also added 6799 for the test CC number 6799990100000000019, but that may not be suitable for your needs.