** Nucleus Information Service Inc.
** Author: J.A.Greant ( firstname.lastname@example.org )
** Date : October 28, 1999
** This function is based on similar functions by:
** Brett Error (email@example.com)
** mcmer (firstname.lastname@example.org)
** Major differences include:
** Cleaner, easier-to-read validation algorithm.
** Returns an error message.
** Returns the type of card based on the number provided.
** This function returns an associative array that will contain the associative keys
** valid: a true or false result indicating if the card is valid or not
** type : a string containing the type of the credit card
** : may return 'American Express','Mastercard','Visa' OR 'unknown'
** The array may also contain a key named error.
** error: a string containing an error message indicating the reason why the card number was invalid.
** This function only validates Amex, Mastercard and Visa numbers.
** The method of validation is based on the Luhn check
** (see http://www.beachnet.com/~hstiles/cardtype.html)
** If you modify this code or if you find an error in the code, please write me!
** Zak Greant (email@example.com)
** This code is provided "As Is" with no warrantees express or implied.
** The author, his employer and any contributors are not liable for
** any damages resulting from the use of this code.
FUNCTION credit_card_validator( $credit_card_number )
# Clean out any non-numeric characters in $credit_card_number
$credit_card_number = EREG_REPLACE( '[^0-9]','', $credit_card_number );
$ccn_length = STRLEN( $credit_card_number );
# Find the type of the card based on the prefix and length of the card number
IF( EREG( '^3[4|7]', $credit_card_number ) && $ccn_length == 15 )
$type = 'American Express';
ELSE IF( EREG( '^4', $credit_card_number ) && ( $ccn_length == 13 || $ccn_length == 16 ) )
$type = 'Visa';
ELSE IF( EREG( '^5[1-5]', $credit_card_number ) && $ccn_length == 16 )
$type = 'Mastercard';
RETURN ARRAY( 'valid' => false, 'type' => 'unknown', 'error' => 'This is not a valid number for an American Express, Mastercard or Visa Card. Please re-enter the number or use a different card.' );
# Reverse the credit card number
$x = STRREV( $credit_card_number );
# Loop through the reversed credit card number one digit at a time. Transform odd numbered entries and sum with even entries
FOR( $i = 0; $i < $ccn_length ; $i++ )
IF( $i % 2 ) # Test to see if the current string index ( $i ) is odd.
$sum += ( ( $x[ $i ] % 5 ) * 2 ) + FLOOR( ( $x[ $i ] / 5 ) ); # This formula is equivalent to multiplying a number by two and then, if the result has two digits, summing the two digits.
$sum += $x[ $i ];
IF( ! ( $sum % 10 ) ) # If the result, divided by 10 has no fractional remainer, then the card # is valid.
RETURN ARRAY( 'valid' => true, 'type' => $type );
RETURN ARRAY( 'valid' => false, 'type' => $type, 'error' => "This is not a valid number for a $type Card. Please re-enter the card number or use a different card." );
Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.