Need to validate credit card number formats before processing? This guide covers the Credit Card Regex API, which provides patterns for validating card number structure and detecting card types like Visa, Mastercard, Amex, and Discover.
Card Number Structure
Credit card numbers follow specific patterns. Each card type has a distinct prefix (IIN/BIN) and length:
- Visa: Starts with 4, 13-16 digits
- Mastercard: Starts with 51-55 or 2221-2720, 16 digits
- American Express: Starts with 34 or 37, 15 digits
- Discover: Starts with 6011, 622126-622925, 644-649, or 65, 16 digits
Detecting Card Types
Card type detection is useful for displaying the appropriate card logo and applying format-specific validation. The API can return patterns for specific card types or a general pattern that matches any major card.
Using the Credit Card Regex API
TinyFn provides credit card validation patterns:
GET https://api.tinyfn.io/v1/regex/patterns/credit-card
Headers: X-API-Key: your-api-key
{
"patterns": {
"any": "^(?: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})$",
"visa": "^4[0-9]{12}(?:[0-9]{3})?$",
"mastercard": "^5[1-5][0-9]{14}$",
"amex": "^3[47][0-9]{13}$",
"discover": "^6(?:011|5[0-9]{2})[0-9]{12}$"
},
"description": "Credit card number format patterns (digits only)",
"note": "Use Luhn algorithm for checksum validation"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
type |
string | Card type: any, visa, mastercard, amex, discover. Default: any |
allow_spaces |
boolean | Allow spaces in the pattern (e.g., "4111 1111 1111 1111"). Default: false |
allow_dashes |
boolean | Allow dashes in the pattern (e.g., "4111-1111-1111-1111"). Default: false |
Code Examples
JavaScript / Node.js
// Get credit card patterns
const response = await fetch(
'https://api.tinyfn.io/v1/regex/patterns/credit-card',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const { patterns } = await response.json();
// Detect card type
function detectCardType(number) {
const digits = number.replace(/\D/g, '');
if (new RegExp(patterns.visa).test(digits)) return 'visa';
if (new RegExp(patterns.mastercard).test(digits)) return 'mastercard';
if (new RegExp(patterns.amex).test(digits)) return 'amex';
if (new RegExp(patterns.discover).test(digits)) return 'discover';
return 'unknown';
}
console.log(detectCardType('4111111111111111')); // "visa"
Python
import requests
import re
response = requests.get(
'https://api.tinyfn.io/v1/regex/patterns/credit-card',
headers={'X-API-Key': 'your-api-key'}
)
patterns = response.json()['patterns']
def detect_card_type(number):
digits = re.sub(r'\D', '', number)
if re.match(patterns['visa'], digits):
return 'Visa'
if re.match(patterns['mastercard'], digits):
return 'Mastercard'
if re.match(patterns['amex'], digits):
return 'American Express'
if re.match(patterns['discover'], digits):
return 'Discover'
return 'Unknown'
print(detect_card_type('5500 0000 0000 0004')) # "Mastercard"
cURL
curl "https://api.tinyfn.io/v1/regex/patterns/credit-card?type=visa&allow_spaces=true" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Card Type Detection: Show appropriate card logo during input
- Format Validation: Validate card format before submission
- Input Masking: Auto-format card numbers as user types
- Form Pre-validation: Catch format errors before server round-trip
- POS Systems: Validate card numbers at point of sale
Best Practices
- Use Luhn algorithm: Regex validates format; Luhn validates checksum
- Strip non-digits: Remove spaces and dashes before validation
- Never store full card numbers: Only store last 4 digits for reference
- Use payment processor: Final validation happens at the payment gateway
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-regex": {
"url": "https://api.tinyfn.io/mcp/regex/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all regex tools available via MCP in our Regex MCP Tools for AI Agents guide.
Try the Credit Card Regex API
Get your free API key and start validating card formats.
Get Free API Key