Regex for Credit Card Number — VISA, MasterCard, AmEx
Regex patterns for credit card detection by issuer: VISA, MasterCard, American Express, Discover, JCB, and Diners. Combine with the Luhn check for full validation.
Detailed Explanation
Regex for Credit Card Numbers
Credit card numbers follow ISO/IEC 7812 with issuer-specific BIN ranges. Regex can identify the issuer and length, but it cannot replace the Luhn checksum, which catches most typos.
Issuer Patterns (digits only, no separators)
| Issuer | Pattern |
|---|---|
| VISA | ^4[0-9]{12}(?:[0-9]{3})?$ |
| MasterCard | `^(?:5[1-5][0-9]{14} |
| American Express | ^3[47][0-9]{13}$ |
| Discover | `^6(?:011 |
| JCB | `^(?:2131 |
| Diners Club | `^3(?:0[0-5] |
Combined Detector
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$
Handling User Input with Spaces or Dashes
Strip non-digits first, then validate:
const digits = input.replace(/\D/g, "");
if (/^4[0-9]{12}(?:[0-9]{3})?$/.test(digits)) ...
Tested Examples
| Input | Detected Issuer |
|---|---|
4111111111111111 |
VISA |
5500000000000004 |
MasterCard |
378282246310005 |
American Express |
6011111111111117 |
Discover |
30569309025904 |
Diners Club |
Why You Still Need Luhn
4111111111111112 matches the VISA regex but fails Luhn. Always run the Luhn algorithm after format detection to reject typos.
PCI DSS Reminder
Never log raw card numbers. Mask all but the last four digits as soon as you have classified the issuer.
Use Case
Detecting card brand to show the correct logo on a checkout form, or scanning support transcripts and database dumps for accidentally-stored PAN data that violates PCI DSS.