PHP Credit Card Validation Boolean

By Koolvin on Jul 10, 2011

This function will validate all USA credit cards numbers, all it really does is verify the numbers, not the actual format.

example:
if (!validateCC($_POST['creditcard'])) {
echo "OOPS! Invalid Credit Card Number!;
}

<?
function validateCC($in) {
    if (strlen($in) < 16 || $strlen($in) > 13) { 
        $num = str_split(strrev($in));
        $f = 0;
        foreach ($num as $k => $n) {        
            if ($k % 2 != 0) { $n = 2*$n; }
            foreach (str_split($n) as $h) { $f+=$h; }
        }
        if ($f % 10 == 0) {return true;}
    }
    return false;
}
?>

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.