Regex for credit card
Card numbers are 13 to 19 digits; remove spaces and dashes first, then check ^\d{13,19}$ and run the Luhn checksum. To detect the brand: Visa ^4\d{12}(?:\d{3})?$, Mastercard ^(?:5[1-5]\d{2}|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)\d{12}$, Amex ^3[47]\d{13}$, Discover ^6(?:011|5\d{2})\d{12}$.
Syntax
/^\d{13,19}$/ (after removing spaces and dashes)Examples
"4111 1111 1111 1111".replace(/[\s-]/g, "")Result: "4111111111111111"
Normalize before testing.
/^4\d{12}(?:\d{3})?$/.test("4111111111111111")Result: true
Visa: starts with 4, 13 or 16 digits.
const luhn = n => [...n].reverse().reduce((s, d, i) => s + (i % 2 ? (d * 2 > 9 ? d * 2 - 9 : d * 2) : +d), 0) % 10 === 0;
luhn("4111111111111111")Result: true
The Luhn checksum catches typos that a regex cannot.
"4111111111111111".replace(/\d(?=\d{4})/g, "*")Result: "************1111"
Mask every digit that still has four digits after it.
Common mistakes
- Validating with the regex alone; a string of digits that matches the brand pattern can still be a typo, so always run Luhn.
- Testing the raw input with spaces, so 4111 1111 1111 1111 fails; strip [\s-] first.
- Assuming all cards are 16 digits; Amex is 15 and Visa can be 13 or 19.
- Logging or storing the full number while debugging; keep only the last four digits and let your payment provider handle the rest.
Practice Regex for credit card
These drills check your answer on live data, free, no account needed.
- Keep only the digitsRemove every character that is not a digit, so phone numbers in any format become plain digits.
- Lookahead: amounts before USDMatch every number (with optional decimals) that is followed by a space and USD. The whole match is compared, so it must not include the space or USD.
- Alternation: cat, dog or birdThe whole line must match. Match exactly one of the words cat, dog or bird.
Questions
Does a regex verify that a credit card is real?
No. It checks the shape and brand prefix. The Luhn check catches most typos, and only the payment processor can confirm the card exists and has funds.
What is the regex for an expiry date MM/YY?
^(0[1-9]|1[0-2])\/\d{2}$ matches 03/27. Compare the parsed month and year against today to reject expired cards.
What is the regex for a CVV?
^\d{3,4}$ : three digits for most brands, four for American Express. Never store it.
Related
- Regex for numbers onlyThe ^\d+$ pattern for digits-only strings, stripping everything that is not a digit, extracting numbers from text, and patterns for signed and decimal values.
- Lookahead (?=...) and (?!...)How lookahead checks what follows without consuming it: numbers before a unit, words without a prefix, thousands separators and file name filters.
- Alternation |How the pipe works, why ^cat|dog$ does not do what it looks like, how to group alternatives, and why option order matters when one is a prefix of another.
- Regex for phone numberPhone number regex for US numbers with dashes, dots, spaces or parentheses, an E.164 check for international input, and how to normalize before validating.