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
Email Address /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ URL /^https?:\/\/(www\.)?[a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_+.~#?&/=-]*)$/ Phone Number (International) /^\+?[1-9]\d{1,14}$/ Phone Number (US) /^\(?[2-9]\d{2}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/ Phone Number (EU) /^\+?[1-9][0-9]{0,3}[\s.-]?\(?[0-9]{1,5}\)?[\s.-]?[0-9]{1,5}[\s.-]?[0-9]{1,5}$/ IPv4 Address /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/
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.