<?php

$number= $_POST['creditcardnumber'];


function validatecard($number){
    global $type;

    $cardtype = array(
        "Visa"       => "/^4[0-9]{12}(?:[0-9]{3})?$/",
        "Mastercard" => "/^5[1-5][0-9]{14}$/",
        "Amex"       => "/^3[47][0-9]{13}$/",
        "Discover"   => "/^6(?:011|5[0-9]{2})[0-9]{12}$/",
    );

    if (preg_match($cardtype['Visa'],$number))
    {
	$type= "Visa";
        return 'Visa';
	
    }
    else if (preg_match($cardtype['Mastercard'],$number))
    {
	$type= "Mastercard";
        return 'Mastercard';
    }
    else if (preg_match($cardtype['Amex'],$number))
    {
	$type= "Amex";
        return 'Amex';
	
    }
    else if (preg_match($cardtype['Discover'],$number))
    {
	$type= "Discover";
        return 'Discover';
    }
    else
    {
        return false;
    } 
 }

validatecard($number);



if (validatecard($number) !== false)
{
echo "<input type='hidden' name='cardType' value='$type' />";
}
else
{
echo "<span style='position:relative;top:-8px;font-size:12px;color:#ff0000;'>Invalid card..Please check your card</span>";
}

?>
