Credit Card Regex Python Validator

Search...

⌘K

Credit Card Regex Python Validator

Search...

⌘K


Credit Card Regex Python Validator

Credit Card Regex Python Validator

Use the Credit Card Regex Python Validator to instantly test card number formats using Python regex. Whether you’re working on payment gateways or form validations, this tool checks if card numbers match valid patterns. For better input validation, try it alongside the Email Regex Python Validator or Password Regex Python Validator for secure, all-in-one user verification.

4111-1111-1111-1111
Possible security issues
This regex appears to be safe.
Explanation
  • [A-Z]: uppercase letters
  • [a-z]: lowercase letters
  • [0-9]: digits
  • \.: a literal dot
  • +: one or more of the preceding
  • *: zero or more of the preceding
  • ?: optional (zero or one)
  • ^: start of string
  • $: end of string
Match information
Match 1: "4111-1111-1111-1111" at index 0
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

What is Credit Card Regex?


Credit card numbers follow defined structures based on the issuer (Visa, MasterCard, Amex, etc.). They typically contain 13–19 digits and may start with specific prefixes:


  • Visa: Begins with 4, 13 or 16 digits

  • MasterCard: Starts with 51–55 or 2221–2720, 16 digits

  • American Express: Starts with 34 or 37, 15 digits


A regex pattern helps validate if the input looks like a valid card number—not if it is real or active.


Credit Card Regex Pattern in Python


A regex pattern to match major card types looks like this:

^(?:4[0-9]{12}(?:[0-9]{3})?|      # Visa  
5[1-5][0-9]{14}|                 # MasterCard  
3[47][0-9]{13}|                  # American Express  
6(?:011|5[0-9]{2})[0-9]{12})$    # Discover


This pattern:

  • Validates 13–16 digits

  • Confirms prefix for the card type

  • Does not allow characters or separators


How to Validate Credit Cards Using Regex in Python


Here’s a complete Python example to check if a card number string matches:

import re

def is_valid_credit_card(card_number):
    pattern = re.compile(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})$')
    return bool(pattern.fullmatch(card_number))

# Test
cards = [
    "4111111111111111",  # Visa - valid
    "5500000000000004",  # MasterCard - valid
    "378282246310005",   # Amex - valid
    "1234567890123456"   # Invalid
]

for c in cards:
    print(f"{c} -> {is_valid_credit_card(c)}")


Use Cases


  • Payment Gateways: Ensure only validly formatted card numbers are submitted in checkout forms.

  • Form Input Validation: Catch typos or fake card numbers before attempting backend verification.

  • Pre-validation for APIs: Reduce API load by screening inputs early.

  • Data Entry Systems: Prevent incorrect card number entries in CRM or financial platforms.


Enhance this validation by using the Phone Number Regex Python Validator for multi-input forms.


Pro Tips


  • This regex only checks format, not real-time validity or card activity.

  • Don’t store credit card numbers in plaintext. Always encrypt or tokenize.

  • Avoid separators like hyphens or spaces unless you handle them explicitly.

  • If you’re validating pasted inputs, strip whitespace using str.replace(" ", "").

  • Combine it with Password Regex Python Validator for secure signup or payment flows.


Frequently asked questions

Does this regex confirm if the card is real or active?×
No. It only validates the format and pattern—not if the card exists.
Is this regex PCI compliant?+
Can I use this for CVV or expiry checks?+
What if I want to allow spaces or dashes in card numbers?+
Does it cover all card types?+