Credit Card Number Validation Regex
Validate Visa, Mastercard, and American Express credit card numbers with proper formatting.
Pattern & Test String
About This Pattern
Pattern: ^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})$
Flags: g
Results
Enter a regex pattern to see the results here.
Frequently Asked Questions
Which card types does this pattern support?
This pattern matches Visa (starting with 4, 13-16 digits), Mastercard (starting with 51-55, 16 digits), and American Express (starting with 34 or 37, 15 digits).
Does this validate the card checksum (Luhn algorithm)?
No, regex only validates format. For full validation including the Luhn checksum, use a dedicated credit card validation library.
Should I store credit card numbers?
No! Storing credit card numbers requires PCI DSS compliance. Use tokenization services like Stripe or PayPal instead.
Practical Examples
Payment Form Validation
Basic format validation before submitting to payment gateway.
Card Type Detection
Identify card type to show appropriate logo or apply rules.
Test Data Generation
Validate test card numbers in development environments.
Common Issues & Solutions
This pattern doesn't validate spaces or dashes. Remove them before testing: str.replace(/[-\s]/g, '')
Always use Luhn algorithm validation in addition to regex for production use.
Never log or store full credit card numbers. This violates PCI DSS compliance.
For Discover cards, add: |6(?:011|5[0-9]{2})[0-9]{12}