Validation function for LUHNMod10 and variant. Can discriminate credit card

Validation function for LUHNMod10 and variant. Can discriminate credit card
numbers of varying lengths.

<?
function prestoLUHN($ccn=”",$alt=”0″)
{    
// pre-validations
$ccn = ereg_replace(“[^[:digit:]]+”, “”, $ccn);
if ((strlen($ccn) < 1)) return FALSE;

// Mapping: Double-then-SumOfDigits  (0123456789) => (0246813579)
for($i=0;$i<=9;$i++) $v[$i] = (($i*2)<9) ? ($i*2):($i*2)-9;

// Initial value of $alt (Alternation Pattern) determines which digits are transformed by $v[] during checksum calculation
// 0 = apply transform to even-numbered digits from LSD-to-MSD (Conventional LUHNMod10)
// 1 = apply transform to  odd-numbered digits from LSD-to-MSD (alternative)

$checksum = 0;     
// Calculate checksum from the end of the string (the low-order digit) to beginning of string
for($i = strlen($ccn)-1; $i >= 0; $i–)
{
   $digit = substr($ccn, $i, 1);
   $checksum += $alt ? $v[$digit] : $digit;
   $alt ^= 1;
}

// return the checksum descrimination result
return !($checksum % 10);      
};         
?>

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.