Credit Card Type provides a utility for determining credit card types from both complete and partial card numbers. Designed for type-as-you-go detection, it supports progressive card type identification as users enter their card information, making it ideal for payment form validation and UI enhancement.
npm install credit-card-typeimport creditCardType from "credit-card-type";For CommonJS:
const creditCardType = require("credit-card-type");Note: This module uses CommonJS export syntax (export = creditCardType), so TypeScript imports should use the default import syntax shown above.
import creditCardType from "credit-card-type";
// Detect from partial number (type-as-you-go)
const partialResults = creditCardType("4111");
console.log(partialResults[0].type); // 'visa'
console.log(partialResults[0].niceType); // 'Visa'
// Multiple matches with ambiguous input
const ambiguousResults = creditCardType("6");
console.log(ambiguousResults.length); // 6 (multiple card types start with 6)
console.log(ambiguousResults[0].niceType); // 'Discover'
console.log(ambiguousResults[1].niceType); // 'UnionPay'
// Complete number detection
const completeResults = creditCardType("4111111111111111");
console.log(completeResults[0].gaps); // [4, 8, 12] - formatting positions
console.log(completeResults[0].lengths); // [16, 18, 19] - valid lengths
console.log(completeResults[0].code); // { name: 'CVV', size: 3 }Determines credit card types from partial or complete card numbers.
/**
* Determines credit card types from a card number string
* @param cardNumber - Card number string (should be normalized, no spaces/letters)
* @returns Array of matching card types, empty array if no matches
*/
function creditCardType(cardNumber: string): Array<CreditCardType>;Gets detailed information about a specific card type.
/**
* Returns card configuration for a specific card type
* @param cardType - Card type identifier (e.g., 'visa', 'mastercard')
* @returns Card configuration object or undefined if invalid type
*/
function getTypeInfo(cardType: string): CreditCardType | undefined;Add, remove, update, and reorder card types for custom detection scenarios.
/**
* Adds a custom card type to the detection system
* @param config - Card configuration object
*/
function addCard(config: CreditCardType): void;
/**
* Removes a card type from detection
* @param name - Card type name to remove
*/
function removeCard(name: string): void;
/**
* Updates an existing card type configuration
* @param cardType - Card type identifier to update
* @param updates - Partial configuration object with updates
*/
function updateCard(cardType: string, updates: Partial<CreditCardType>): void;
/**
* Changes the priority order for card type detection
* @param name - Card type name to reorder
* @param position - New position in the detection order (0-based)
*/
function changeOrder(name: string, position: number): void;
/**
* Resets all custom modifications to original state
*/
function resetModifications(): void;Named constants for all supported card types.
const types: {
VISA: "visa";
MASTERCARD: "mastercard";
AMERICAN_EXPRESS: "american-express";
DINERS_CLUB: "diners-club";
DISCOVER: "discover";
JCB: "jcb";
UNIONPAY: "unionpay";
VERVE: "verve";
MAESTRO: "maestro";
ELO: "elo";
MIR: "mir";
HIPER: "hiper";
HIPERCARD: "hipercard";
};interface CreditCardType {
/** Human-readable card brand name (e.g., "Visa", "Mastercard") */
niceType: string;
/** Code-friendly card type identifier (e.g., "visa", "mastercard") */
type: string;
/** Number patterns for matching: single numbers, ranges [min, max], or mixed arrays */
patterns: (number | [number, number])[];
/** Expected gap positions for formatting (e.g., [4, 8, 12] for "1234 5678 9012 3456") */
gaps: number[];
/** Valid card number lengths (excluding spaces) */
lengths: number[];
/** Security code information */
code: {
/** Security code name (e.g., "CVV", "CVC", "CID") */
name: string;
/** Security code length (3 or 4 digits) */
size: number;
};
/** Pattern matching strength (set internally during detection) */
matchStrength?: number;
}
type CreditCardTypeCardBrandId =
| "american-express"
| "diners-club"
| "discover"
| "elo"
| "hiper"
| "hipercard"
| "jcb"
| "verve"
| "maestro"
| "mastercard"
| "mir"
| "unionpay"
| "visa";The library includes built-in support for 13 major credit card brands:
| Brand | Type ID | Nice Type | Patterns | Security Code | Lengths |
|---|---|---|---|---|---|
| Visa | visa | Visa | 4 | CVV (3) | 16, 18, 19 |
| Mastercard | mastercard | Mastercard | 51-55, 2221-2720 | CVC (3) | 16 |
| American Express | american-express | American Express | 34, 37 | CID (4) | 15 |
| Diners Club | diners-club | Diners Club | 300-305, 36, 38, 39 | CVV (3) | 14, 16, 19 |
| Discover | discover | Discover | 6011, 644-649, 65 | CID (3) | 16, 19 |
| JCB | jcb | JCB | 2131, 1800, 3528-3589 | CVV (3) | 16, 17, 18, 19 |
| UnionPay | unionpay | UnionPay | 620, 62100-626999, etc. | CVN (3) | 14-19 |
| Maestro | maestro | Maestro | Various patterns | CVC (3) | 12-19 |
| Elo | elo | Elo | Various specific patterns | CVE (3) | 16 |
| Mir | mir | Mir | 2200-2204 | CVP2 (3) | 16-19 |
| Hiper | hiper | Hiper | Specific patterns | CVC (3) | 16 |
| Hipercard | hipercard | Hipercard | 606282 | CVC (3) | 16 |
| Verve | verve | Verve | Various patterns | CVV (3) | 16, 18, 19 |
Card detection uses sophisticated pattern matching:
4 matches all Visa cards)[51, 55] matches Mastercard 51xxxx-55xxxx)import creditCardType from "credit-card-type";
// Add a custom card type
creditCardType.addCard({
niceType: "Custom Bank Card",
type: "custom-card",
patterns: [9999, [8888, 8889]],
gaps: [4, 8, 12],
lengths: [16],
code: {
name: "CVV",
size: 3,
},
});
// Update existing card type
creditCardType.updateCard(creditCardType.types.VISA, {
niceType: "Visa Custom",
lengths: [13, 16, 19], // Add support for old 13-digit Visas
});
// Change detection priority
creditCardType.changeOrder("custom-card", 0); // Highest priority
// Reset all modifications
creditCardType.resetModifications();import creditCardType from "credit-card-type";
function formatCardNumber(cardNumber: string, cardType?: string): string {
if (!cardType) {
const results = creditCardType(cardNumber);
if (results.length === 0) return cardNumber;
cardType = results[0].type;
}
const typeInfo = creditCardType.getTypeInfo(cardType);
if (!typeInfo) return cardNumber;
const { gaps } = typeInfo;
const offsets = [0, ...gaps, cardNumber.length];
const components = [];
for (let i = 0; offsets[i] < cardNumber.length; i++) {
const start = offsets[i];
const end = Math.min(offsets[i + 1], cardNumber.length);
components.push(cardNumber.substring(start, end));
}
return components.join(" ");
}
// Usage
formatCardNumber("4111111111111111", "visa"); // "4111 1111 1111 1111"
formatCardNumber("378282246310005", "american-express"); // "3782 822463 10005"import creditCardType from "credit-card-type";
// Only allow specific card types
const allowedTypes = [creditCardType.types.VISA, creditCardType.types.MASTERCARD];
function getAcceptedCardTypes(cardNumber: string) {
return creditCardType(cardNumber).filter((card) =>
allowedTypes.includes(card.type as any)
);
}