PHP Validate Credit Card Number

PHP Validate Credit Card Number :

PHP Validate Credit Card - TechniqZone

 

Now a days , credit card purchase is happened in many websites. The below php function is used to validate the given credit card number in php.

function isCreditCardValid $number$cctype )
{
    if ( !
is_string $number)
         || !
trim $number ) )
             return 
false;
      
    
$ccs = array (

        // Master Card
        'master' => '/^(5[1-5]{1})(d{14})$/',

        // American Express 
        'amex' => '/^3(4|7)d{13}$/',
        
        // Discover Card 
        'discover' => '/^6(011|22|4|5)(d{12}|d{13}|d{14})$/'
        
        // Visa Card 
        'visa' => '/^4(d{12}|d{15})$/',

        // Dinners Club        
        'dinners' => '/^(305|36|38|54|55)(d{12}|d{14})$/',

        // Carte Blanche         
        'carte' => '/^(30[0-5]{1})(d{11})$/'
        
        // enRoute 
        'enroute' => '/^2(014|149)d{11}$/'
        
        // Laser
        'laser' => '/^6(304|706|771|709)(d{12}|d{13}|d{14}'.
                   '|d{15})$/'
        
        // Visa Electron 
        'visaelec' => '/^4(17500|917|913|844|508)(d{10}|d{12})$/'
    );

    if ( !is_string $cctype )
         || !
array_key_existsstrtolower ($cctype) , $ccs ) )
             return 
false;

    return (bool) preg_match $ccs[$cctype], $number );

 

Example :

<?php

if(isCreditCardValid("371449635398431","amex"))
{
                 echo "Valid";
}
else
{
                 echo "Not Valid";
}

?>

 

Here preg_match concept is used to identify the valid credit card number.

 

Leave a Reply

Your email address will not be published. Required fields are marked *