PHP Credit Card Validation Function

This PHP function checks the validity of a credit card specified by the supplied parameters containing the card number and card type.

The specifications for valid credit cards have been taken from various sources on the web. The commonest credit cards are are supported in this implementation, but more may be added as required. One of the advantages of this routine is the ease with which additional cards may be added, as it is totally data dependent.

Try the routine. Although no attempt is made to capture the input, it is suggested that you don't provide the number of your own credit card. The following are credit card numbers in a valid format:

American Express3400 0000 0000 009
Carte Blanche3000 0000 0000 04
Discover6011 0000 0000 0004
Diner's Club3000 0000 0000 04
enRoute2014 0000 0000 009
JCB2131 0000 0000 0008
MasterCard5500 0000 0000 0004
Solo6334 0000 0000 0004
Switch4903 0100 0000 0009
Visa4111 1111 1111 1111

Select credit card: Enter number:
Please enter credit card details

The function returns true or false, depending on whether the credit card name / number combination is found to be valid. If it is not valid, a numeric code is returned in one parameter, and error text returned in another as may be seen in the demonstration.

The credit card details are held in an array within the function, and additional cards may be readily added. The format of the array is as follows:

array ('name' => 'Visa', 
       'length' => '13,16', 
       'prefixes' => '4',
       'checkdigit' => true)

Download compressed PHP file (2.46 KB)

Note: This routine was updated on the 18th January 2008 to add support for 18 digit Maestro cards. Thanks to David Groom of Vectis Webdesign for pointing out the error.