Find the card type using card number

The requirement is to find out the card type of a credit/card in the backend using the card number entered in the card.

Use the below code to get the card type of a card.

 var cardnumber = '378282246310005';
 var checkcardtype = detectCardType(cardnumber);
 console.error('checkcardtype create', checkcardtype);

//the functiion detectCardType is used to get the card type.
function detectCardType(number) {
    var re = {
        electron: /^(4026|417500|4405|4508|4844|4913|4917)\d+$/,
        maestro: /^(5018|5020|5038|5612|5893|6304|6759|6761|6762|6763|0604|6390)\d+$/,
        dankort: /^(5019)\d+$/,
        interpayment: /^(636)\d+$/,
        unionpay: /^(62|88)\d+$/,
        visa: /^4[0-9]{12}(?:[0-9]{3})?$/,
        mastercard: /^5[1-5][0-9]{14}$/,
        amex: /^3[47][0-9]{13}$/,
        diners: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/,
        discover: /^6(?:011|5[0-9]{2})[0-9]{12}$/,
        jcb: /^(?:2131|1800|35\d{3})\d{11}$/
    }

    for (var key in re) {
        if (re[key].test(number)) {
            console.error('key', key);
            return key
        }
    }
}



The cardtype will get in the variable checkcardtype .

Leave a comment

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