Credit Card Number with Network Detection Regex for JavaScript
/^(?:(4)[0-9]{12}(?:[0-9]{3,6})?|(5[1-5][0-9]{14}|2(?:2[2-9][1-9]|[3-6][0-9]{2}|7[01][0-9]|720)[0-9]{12})|(3[47][0-9]{13})|(3(?:0[0-5]|[68][0-9])[0-9]{11})|(6(?:011|5[0-9]{2})[0-9]{12,15})|((?:2131|1800|35[0-9]{3})[0-9]{11}))$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching credit card number with network detection, ported and verified for JavaScript. Identity and credential patterns need both correctness and safety, since they're frequent targets for adversarial input. The snippet below is ready to drop into your JavaScript project — whether you're validating in an Express middleware, a Next.js API route, or a client-side form.
Javascript Implementation
// Credit Card Number with Network Detection
// ReDoS-safe | RegexVault — Identity & PII > Financial PII
const creditCardNumberWithNetworkDetectionRegex = /^(?:(4)[0-9]{12}(?:[0-9]{3,6})?|(5[1-5][0-9]{14}|2(?:2[2-9][1-9]|[3-6][0-9]{2}|7[01][0-9]|720)[0-9]{12})|(3[47][0-9]{13})|(3(?:0[0-5]|[68][0-9])[0-9]{11})|(6(?:011|5[0-9]{2})[0-9]{12,15})|((?:2131|1800|35[0-9]{3})[0-9]{11}))$/;
function validateCreditCardNumberWithNetworkDetection(input: string): boolean {
return creditCardNumberWithNetworkDetectionRegex.test(input);
}
// Example
console.log(validateCreditCardNumberWithNetworkDetection("4111111111111111")); // trueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
4111111111111111 | 1234567890123456 |
5500005555555559 | 411111111111 |
378282246310005 | 411111111111111111111 |
30569309025904 | — |
6011111111111117 | — |
When to use this pattern
This pattern is drawn from the Identity & PII > Financial PII category and carries a ReDoS-safe certification. That matters for JavaScript developers because especially critical in long-running Node.js event loops where a ReDoS vulnerability can block the entire process. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
PCI-DSS requires Luhn validation, not just format matching. Storage of card data requires quarterly security assessments and annual PCI-DSS certification. Use a payment gateway (Stripe, Adyen) to avoid handling raw PANs.
Technical Notes
Capture groups: 1=Visa, 2=Mastercard, 3=Amex, 4=Diners Club, 5=Discover, 6=JCB. PCI-DSS: never store CVV, never log PANs, always encrypt stored card data, use tokenization for recurring billing.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern