Credit Card Validation Regex (Discover)
• CronOS Team
regexcredit-cardvalidationtutorialdiscoverpayment
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!
Credit Card Validation Regex (Discover)
Validate Discover credit card numbers starting with 6011 or 65 and having 16 digits using regex pattern.
Pattern Breakdown
regex
^6(?:011|5[0-9]{2})[0-9]{12}$
Components
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
6 | Discover prefix | Must start with 6 |
(?:011|5[0-9]{2}) | Prefix options | Either 6011 or 65xx |
011 | 6011 prefix | Literal 011 (for 6011) |
| | OR operator | Alternation |
5[0-9]{2} | 65xx prefix | 5 followed by two digits (6500-6599) |
[0-9]{12} | Remaining digits | Exactly 12 more digits (16 total) |
$ | End anchor | Ensures match to string end |
Detailed Breakdown
6- Literal 6 (Discover starts with 6)(?:011|5[0-9]{2})- Prefix alternation:011- For cards starting with 60115[0-9]{2}- For cards starting with 65xx (6500-6599)
[0-9]{12}- Remaining 12 digits
Examples
Valid:
6011111111111111(starts with 6011)6500000000000000(starts with 6500)6555555555555555(starts with 6555)6599999999999999(starts with 6599)
Invalid:
4111111111111111(starts with 4, Visa)601111111111111(only 15 digits)60111111111111111(17 digits)6400000000000000(starts with 64, not Discover)6-0111-1111-1111-1111(contains formatting)
Implementation
JavaScript
javascript
const discoverRegex = /^6(?:011|5[0-9]{2})[0-9]{12}$/;
discoverRegex.test('6011111111111111'); // true
discoverRegex.test('6500000000000000'); // true
discoverRegex.test('4111111111111111'); // false (Visa)
discoverRegex.test('601111111111111'); // false (only 15 digits)
// To handle formatted numbers, remove formatting first
const formatted = '6011-1111-1111-1111'.replace(/[-\s]/g, '');
discoverRegex.test(formatted); // true
Python
python
import re
discover_regex = r'^6(?:011|5[0-9]{2})[0-9]{12}$'
bool(re.match(discover_regex, '6011111111111111')) # True
bool(re.match(discover_regex, '6500000000000000')) # True
bool(re.match(discover_regex, '4111111111111111')) # False (Visa)
# To handle formatted numbers
formatted = '6011-1111-1111-1111'.replace('-', '').replace(' ', '')
bool(re.match(discover_regex, formatted)) # True
Go
go
discoverRegex := regexp.MustCompile(`^6(?:011|5[0-9]{2})[0-9]{12}$`)
discoverRegex.MatchString("6011111111111111") // true
discoverRegex.MatchString("6500000000000000") // true
discoverRegex.MatchString("4111111111111111") // false (Visa)
Limitations
- No Luhn algorithm: Doesn't validate using Luhn checksum algorithm
- No formatting: Doesn't accept formatted numbers (spaces, dashes)
- Format only: Validates format, not actual card validity
- No expiration/CVV: Doesn't validate expiration date or CVV
- Security: Never store full card numbers, only last 4 digits
- Limited prefixes: Discover also issues cards with other prefixes (622126-622925, 644-649, etc.) not covered
When to Use
- Initial format validation
- Quick card type detection
- Client-side validation (first pass)
- When you need to identify Discover cards
For production, consider:
- Implementing Luhn algorithm for checksum validation
- Supporting additional Discover prefixes (622126-622925, 644-649, etc.)
- Supporting formatted input (remove spaces/dashes)
- Using payment processing libraries (Stripe, PayPal, etc.)
- Never storing full card numbers
- Server-side validation (never trust client-side only)
- PCI DSS compliance requirements
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!