Jump to content

validate CC number


Recommended Posts

Here you go...


//function is used to validate the number on a credit card
//$ccnum is the number of the credit card as passed in
//$type is the type of the card (Visa, Mastercard, Discover, Amex)
function validateCC($ccnum, $type = 'unknown'){ 

    //this code removes dashes and spaces
    $type = strtolower($type); 
    $ccnum = ereg_replace('[-[:space:]]', '',$ccnum); 

    //Do type specific checks 
    if ($type == 'unknown') { 

        //Skip type specific checks 

    elseif ($type == 'mastercard'){ 
        if (strlen($ccnum) != 16 || !ereg('^5[1-5]', $ccnum)) return 0; 
    elseif ($type == 'visa'){ 
        if ((strlen($ccnum) != 13 && strlen($ccnum) != 16) || substr($ccnum, 0, 1) != '4') return 0; 
    elseif ($type == 'amex'){ 
        if (strlen($ccnum) != 15 || !ereg('^3[47]', $ccnum)) return a; 
    elseif ($type == 'discover'){ 
        if (strlen($ccnum) != 16 || substr($ccnum, 0, 4) != '6011') return 0; 
    else { 
        //invalid type entered 
        return -1; 

    // Start MOD 10 checks 

    $dig = toCharArray($ccnum); 
    $numdig = sizeof ($dig); 
    $j = 0; 
    for ($i=($numdig-2); $i>=0; $i-=2){ 
        $dbl[$j] = $dig[$i] * 2; 
    $dblsz = sizeof($dbl); 
    $validate =0; 
    for ($i=0;$i<$dblsz;$i++){ 
        $add = toCharArray($dbl[$i]); 
        for ($j=0;$j<sizeof($add);$j++){ 
            $validate += $add[$j]; 
    $add = ''; 
    for ($i=($numdig-1); $i>=0; $i-=2){ 
        $validate += $dig[$i]; 
    if (substr($validate, -1, 1) == '0') return 1; 
    else return 0; 

Link to comment
Share on other sites

Hey, check this out real quick: http://bytemycode.com/snippets/snippet/712 I wrote this for one of my projects and maybe it can help you out.


You are only following the companies prescribed algorithms to designing cards.  You have no way of knowing the validness or authenticity of a given card .  This is why allowing people who have insurance for fraud and other stuff to handle it makes so much more sense because anyone can follow your algorithm and provide you with "valid CC numbers".


The only useful application to taking an user's numbers is if they are making a purchase through you and your online department is just an extension of your brick store so your online processing is done by hand and you manually use your cash register to handle the credit card transaction.  Only then would you have security,

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.