Credit Card Number Validation

Credit Card Number Validation

This function takes a string of the cardnumber with no blanks, and returns true if the number validates as a creditcard number, else it returns false.

function validate_cardnum($cardnum)
{
$checkdigit=substr($cardnum,-1);
$remainingcardnum=substr($cardnum,0,strlen($cardnum)-1);

$i=0;

while($i < strlen($remainingcardnum))
{
if($i%2==0)
$remaing_array[$i]=substr($remainingcardnum,($i+1)*-1,1) * 2;
else
$remaing_array[$i]=substr($remainingcardnum,($i+1)*-1,1);

if($remaing_array[$i]>=10)
$checksum=$checksum+1;

$checksum=$checksum+($remaing_array[$i]%10);
$i++;
}

$calculatedcheckdigit=(10-($checksum%10))%10;
if( $calculatedcheckdigit==$checkdigit)
return true;
else
return false;
}

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.