Skip to main content
Numbers & Finance · In-Browser Regular Expression Lab

Credit Card Regex — Validate Visa (13/16) and Mastercard (16) Numbers

Detects Visa and Mastercard credit card format with optional space or hyphen delimiters.

Flags:
//none
Match Found
Load Sample:
Match Inspection & Details1 match
Match #1:4111111111111111

Test Samples Verification Matrix

Expected Valid Samples (Should Match)

4111111111111111 Pass
5500000000000004 Pass
2221000000000000 Pass

Expected Invalid Samples (Must Reject)

378282246310005 Rejected (Pass)
411111111111 Rejected (Pass)
9999999999999999 Rejected (Pass)
Code Implementation Generator
// JavaScript / TypeScript (ES6+)
const pattern = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|2(?:22[1-9]|2[3-9][0-9]|[3-6][0-9]{2}|7[01][0-9]|720)[0-9]{12})$/;
const text = "4111111111111111";

const isMatch = pattern.test(text);
console.log("Is Match:", isMatch);

if (isMatch) {
  const matches = text.match(pattern);
  console.log("Matches:", matches);
}

Regular Expression Token Breakdown

Matches Visa starting with 4 (13 or 16 digits) and Mastercard starting with 51-55 or 2221-2720 (16 digits). Each component of the regular expression serves a distinct syntactical purpose as mapped below:

Token PatternName / ComponentFunction & Rule
4[0-9]{12}(?:[0-9]{3})?Visa FormatStarts with 4, total 13 or 16 digits.
5[1-5][0-9]{14}Mastercard LegacyStarts with 51-55, total 16 digits.
2[2-7]...[0-9]{12}Mastercard 2-SeriesStarts with 2221-2720, total 16 digits.

Frequently Asked Questions

Should I also run the Luhn algorithm?
Yes, regex validates prefix and length format, but Luhn checksum validation confirms mathematical validity.
Browse All 40 Patterns