credit card validation using Luhn's

I was working on a credit card validation using Luhn's algorithm: I just need to see if the number is a valid credit card number.

$number=clean($_GET['number']);
if (LuhnCheck($number) == 0)
{
   echo "credit card valid";
}
else
{
   echo "invalid";
}

 function LuhnCheck($strDigits)
    {
        $sum = 0;
        $alt = false;
        for($i = strlen($strDigits) - 1; $i >= 0; $i--) 
        {
            if($alt)
            {
               $temp = $strDigits[$i];
               $temp *= 2;
               $strDigits[$i] = ($temp > 9) ? $temp = $temp - 9 : $temp;
            }
            $sum += $strDigits[$i];
            $alt = !$alt;
        }
        return $sum % 10 == 0;
    }

Will the above work for AMEX,discover,visa and mastercard? I am unable to confirm.


Don't reinvent the wheel when there is great potential for expensive risks.

There are many proven libraries out there - here is one:

http://framework.zend.com/manual/1.12/en/zend.validate.set.html#zend.validate.set.creditcard


According to Wikipedia, the Luhn algorithm is used for all ISO/IED-7812 card numbers. American Express is in the travel and entertainment category, thus its numbers start with 3 . Discover, Visa, and Mastercard are in the banking and financial category, thus their numbers start with 4 or 5 (and Discover is also in merchandising, so their numbers start with 6 as well).

In short, if your code implements the Luhn algorithm, yes, it will work for American Express, Discover, Visa, and Mastercard.


Here is a jquery plugin: https://github.com/iHwy/jQuery-Validation-Extension

And a wiki entry about bank card numbers http://en.wikipedia.org/wiki/Bank_card_numbers.

Edit: and a php script: http://www.braemoor.co.uk/software/creditcard.php

链接地址: http://www.djcxy.com/p/70722.html

上一篇: Javascript中的信用卡预测

下一篇: 使用Luhn's进行信用卡验证