Credit Card Validation Regex (MasterCard)
• CronOS Team
regexcredit-cardvalidationtutorialmastercardpayment
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 (MasterCard)
Validate MasterCard credit card numbers starting with 51-55 and having 16 digits using regex pattern.
Pattern Breakdown
regex
^5[1-5][0-9]{14}$
Components
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
5 | MasterCard prefix | Must start with 5 |
[1-5] | Second digit | Must be 1, 2, 3, 4, or 5 (51-55 range) |
[0-9]{14} | Remaining digits | Exactly 14 more digits (16 total) |
$ | End anchor | Ensures match to string end |
Character Classes
5- Literal 5 (MasterCard starts with 5)[1-5]- Second digit: 1, 2, 3, 4, or 5 (range 51-55)[0-9]- Any digit 0-9{14}- Quantifier: exactly 14 occurrences
Examples
Valid:
5111111111111111(starts with 51)5211111111111111(starts with 52)5311111111111111(starts with 53)5411111111111111(starts with 54)5511111111111111(starts with 55)
Invalid:
4111111111111111(starts with 4, Visa)5611111111111111(starts with 56, outside range)5011111111111111(starts with 50, outside range)511111111111111(only 15 digits)5-1111-1111-1111-1111(contains formatting)
Implementation
JavaScript
javascript
const mastercardRegex = /^5[1-5][0-9]{14}$/;
mastercardRegex.test('5111111111111111'); // true
mastercardRegex.test('5511111111111111'); // true
mastercardRegex.test('4111111111111111'); // false (Visa)
mastercardRegex.test('5611111111111111'); // false (outside range)
// To handle formatted numbers, remove formatting first
const formatted = '5111-1111-1111-1111'.replace(/[-\s]/g, '');
mastercardRegex.test(formatted); // true
Python
python
import re
mastercard_regex = r'^5[1-5][0-9]{14}$'
bool(re.match(mastercard_regex, '5111111111111111')) # True
bool(re.match(mastercard_regex, '5511111111111111')) # True
bool(re.match(mastercard_regex, '4111111111111111')) # False (Visa)
# To handle formatted numbers
formatted = '5111-1111-1111-1111'.replace('-', '').replace(' ', '')
bool(re.match(mastercard_regex, formatted)) # True
Go
go
mastercardRegex := regexp.MustCompile(`^5[1-5][0-9]{14}$`)
mastercardRegex.MatchString("5111111111111111") // true
mastercardRegex.MatchString("5511111111111111") // true
mastercardRegex.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
- Note: MasterCard also issues cards starting with 2 (2-series), not covered by this pattern
When to Use
- Initial format validation
- Quick card type detection
- Client-side validation (first pass)
- When you need to identify MasterCard cards
For production, consider:
- Implementing Luhn algorithm for checksum validation
- Supporting 2-series MasterCard cards (starts with 2)
- 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!