Skip to content

Regex for Credit Card — Pattern, Examples & Caveats

Pattern
^(?: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})$

This pattern matches the major credit-card number formats by issuer prefix and length: Visa (4, 13 or 16 digits), Mastercard (51-55, 16), American Express (34/37, 15) and Discover (6011/65, 16). It is a structural check, not a validity proof. Security-oriented patterns check sensitive or policy-bound input: password composition, card number shapes, tokens, private IP ranges and national identifiers. They are a first line of defense, never the whole story — pair them with checksums, real verification, encryption and access control. A regex can confirm a format but cannot prove a secret is valid or safe. It needs no special flags. Apply it to the whole input as a format contract. It does not run the Luhn checksum, so structurally valid but numerically invalid numbers pass — always apply Luhn for real validation. Spaces and hyphens are rejected; strip separators before matching. Never log or store full card numbers; tokenization and PCI-DSS compliance matter far more than regex validation. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.

Advertisement
Ad slot · ref-top

Token-by-token explanation

  • 4[0-9]{12}(?:[0-9]{3})? — Visa: starts with 4, 13 or 16 digits
  • 5[1-5][0-9]{14} — Mastercard: 51-55 prefix, 16 digits
  • 3[47][0-9]{13} — Amex: 34 or 37 prefix, 15 digits
  • 6(?:011|5[0-9]{2})[0-9]{12} — Discover: 6011 or 65xx, 16 digits
  • (?: ... | ... ) — non-capturing alternation of issuer formats

What it matches

411111111111111155000000000000043400000000000096011000000000004

What it does not match

123456789012345641111111111156000000000000044111-1111-1111-1111

When to use it

Reach for a security pattern as one layer of input handling for sensitive or policy-bound data: password rules, card shapes, tokens, private ranges and national identifiers. Never treat a match as proof of validity or safety — combine it with checksums, signature verification, breach screening, encryption and strict access control. Also guard against ReDoS by avoiding nested quantifiers and bounding input length, since security checks are a tempting target for denial-of-service abuse.

Caveats

  • It does not run the Luhn checksum, so structurally valid but numerically invalid numbers pass — always apply Luhn for real validation.
  • Spaces and hyphens are rejected; strip separators before matching.
  • Never log or store full card numbers; tokenization and PCI-DSS compliance matter far more than regex validation.

Code examples

JavaScript
const re = /^(?: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})$/;
const input = "4111111111111111";
if (re.test(input)) {
  // valid: proceed
} else {
  // reject with a helpful message
}
Python
import re

rx = re.compile(r"^(?: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})$")
value = "4111111111111111"
if rx.fullmatch(value):
    pass  # valid
else:
    raise ValueError("invalid format")
Java
import java.util.regex.*;

Pattern p = Pattern.compile("^(?: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})$");
Matcher m = p.matcher("4111111111111111");
boolean ok = m.matches();   // whole input must conform
PHP
<?php
$subject = "4111111111111111";
if (preg_match('/^(?: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})$/', $subject, $m)) {
    // $m[0] is the full match
}

How to test it

Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.

Frequently asked questions

What is the Luhn check and why add it?

Luhn is a checksum algorithm that detects most single-digit and transposition errors. Regex confirms the issuer/length shape; Luhn confirms the number is arithmetically plausible. Use both.

Does this cover all card brands?

It covers Visa, Mastercard, Amex and Discover. JCB, Diners Club, UnionPay and others need additional alternatives; consider a dedicated payment library for full coverage.

Is a regex enough to secure sensitive input?

Never on its own. Regex checks format; security also needs checksums (Luhn), real verification (token signatures, breach screening), encryption, masking and access control. Treat the pattern as one layer, not the solution.

How do I prevent ReDoS in security checks?

Avoid nested unbounded quantifiers, set a match timeout where the engine supports it, cap input length, and prefer simple linear patterns. Test suspicious inputs with long non-matching strings before deploying.

Advertisement
Ad slot · ref-in-article

Try the Regex Tester

Live regex matching with groups, flags and explanations.

Open tool →