Credit Card Number Regular Expressions

Category: Numbers

A collection of useful Regular Expressions to parse and validate Credit Card Numbers like Visa, MasterCard, American Express, Discover, etc.

There are a lot of regular expressions out there dedicated to parsing and validating credit card numbers. The purpose of this post is to collect a bunch of useful Regular expressions I found in a way that’s easy to use and understand. While this collection isn’t exhaustive, it covers the vast majority of cards you might come across.

All In One:

This is a general regular expression used to match Credit Card Numbers (Visa, MasterCard, American Express, Diners Club, Discover, and JCB cards).

/^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/


Visa:

A regular expression to match Visa card numbers.

/^4[0-9]{12}(?:[0-9]{3})?$/


Mastercard:

A regular expression to match Mastercard numbers.

/^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$/


American Express:

A regular expression to match American Express card numbers.

/^3[47][0-9]{13}$/


Diners Club:

A regular expression to match Diners Club card numbers.

/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/


Discover:

A regular expression to match Discover card numbers.

/^6(?:011|5[0-9]{2})[0-9]{12}$/


JCB:

A regular expression to match JCB card numbers.

/^(?:2131|1800|35\d{3})\d{11}$/


Credit Card Number Examples:

  • Visa: 4012888888881881
  • MasterCard: 5555555555554444
  • American Express: 371449635398431
  • Diners Club: 38520000023237
  • Discover: 6011000990139424
  • JCB: 3566002020360505

Conclusion:

All the Regular Expressions in this blog post have been contributed by our readers. Some of them are contributed from external sources from sites such as Regexlib.com, Stack Overflow, etc.

We aim to keep this resource updated with new and fresh content from our readers. We welcome anyone who wants to contribute a Regular Expression so they can benefit others.

You can submit a bug report for a regex or a bug request for a new regex at Submit Regex.

Rate This Regex
Sending
User Review
0 (0 votes)

Expression Flags

Flags Description
i Ignore case sensitive.
g Allows global search.
m Allows multiline search.