Build a credit card validation regex

Check whether a string has the shape of a payment-card number.

freeworks offlinenothing uploaded
ToolCredit Card Regex
Input
Output
Put this on your own site

The frame below runs the same code as this page, in the reader's own browser. Nothing is sent to us, and nothing is sent to you.

Pick a dark background and the text and panels follow it, so the frame stays readable on a dark page.

Preview

How it works

The pattern accepts digit groups with optional spaces or hyphens and constrains common card lengths; a separate Luhn check can test the checksum. Shape and checksum do not prove an active account.

  • Common 13–19 digit lengths cover major networks.
  • Issuer-specific prefixes are kept out of a brittle static pattern.

Worked example

Visa card number validation pattern
Generate a regex pattern to validate Visa card numbers
Input
											visa
										
Output
												Credit Card Regex (visa):

^4\d{12}(?:\d{3})?$

Visa: starts with 4, 13 or 16 digits
Prefix: 4

Note: This validates format only, not Luhn checksum.
											

When to use this

Checkout forms, payment imports, and card-number test fixtures check card shape.

Edge cases

  • Separators must be removed before checksum testing.
  • A valid Luhn number can still be fabricated or expired.
  • Issuer ranges change over time.