Profile avatar

Chizi Victor

Software Engineer

Credit Card Matcher Regex Fn

Function with regex matchers to obtain different credit/debit card types based on number input.

export function getCardType(value: string) {
    // visa
    const visaRegex = new RegExp('^4[0-9]{12}(?:[0-9]{3})?$');
    if (value.match(visaRegex) != null) return 'Visa';

    // Mastercard
    // Updated for Mastercard 2017 BINs expansion
    const masterCardRegex = new RegExp(
    if (value.match(masterCardRegex) != null) return 'Mastercard';

    // Discover
    const discoverRegex = new RegExp(
    if (value.match(discoverRegex) != null) return 'Discover';

    const americanExpressRegex = new RegExp('^3[47][0-9]{13}$');
    if (value.match(americanExpressRegex) != null) return 'American Express';

    return '';