T
ToolPrime

Regex for Credit Card Number

Validates major credit card numbers (Visa, Mastercard, Amex, Discover).

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})$/

Live Tester

Enter a string to test

Examples

4111111111111111
5500000000000004
378282246310005
1234567890123456
411111111111
0000000000000000

Pattern Breakdown

4[0-9]{12}(?:[0-9]{3})? — Visa (13 or 16 digits)

5[1-5][0-9]{14} — Mastercard (16 digits)

3[47][0-9]{13} — Amex (15 digits)

6(?:011|5[0-9]{2})[0-9]{12} — Discover (16 digits)

Code Snippets

Javascript

const regex = /^(?: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})$/;
regex.test("4111111111111111"); // true

Python

import re
pattern = 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})$"
bool(re.match(pattern, "4111111111111111"))  # True

Php

$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})$/';
preg_match($pattern, "4111111111111111"); // 1

Related Patterns

Frequently Asked Questions

What is the regex for Credit Card Number?
The regex pattern for Credit Card Number is /^(?: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})$/. Validates major credit card numbers (Visa, Mastercard, Amex, Discover).
How do I use the Credit Card Number regex in JavaScript?
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the Credit Card Number pattern match?
It matches strings like 4111111111111111; it rejects strings like 1234567890123456. See the Examples section above for the full list of matching and non-matching cases.

Related Tools