Learn how to validate credit card numbers like Mastercard, Visa Card, and American Express with regular expressions.

When to Use RegEx to Validate a Credit Card Number?

The third-party services charge for each transaction attempt. Whether the transaction is successful or not, you need to pay the charges. In such cases, you want to decline credit card numbers that are clearly invalid. You can quickly validate credit card numbers using regular expressions.

But you cannot completely rely on regular expression validation for your payment feature. Card issuers regularly change their card number patterns (introducing new patterns or withdrawing old ones), so the regex method is not the most robust solution. Although you can try to keep on top of the card patterns from a source like Wikipedia that’s frequently updated.

You can also use regex to quickly find the card brand that you can then use to display logos or labels.

Regular expressions have several practical use cases. The Linux grep command is probably the most common practical use case of regex.

RegEx to Validate Mastercard Number

A Mastercard number is valid if it satisfies the following conditions:

The string should not contain any special characters, alphabets, or whitespaces. The number of characters must be equal to 16. The string should start with either a 2-digit number range (ranging from 51 to 55) or a 4-digit number range (ranging from 2221 to 2720). If the string starts with a 2-digit number range (ranging from 51 to 55), the next 14 digits must be a number between 0 to 9. If the string starts with a 4-digit number range (ranging from 2221 to 2720), the next 12 digits must be a number between 0 to 9.

The following regex satisfies the above conditions and can validate a Mastercard number:

If you are not comfortable with the above expression, check out a beginner’s guide to regular expressions first.

You can use the above regex to validate the Mastercard number in any programming language. Here’s how to do so in Python:

Running this code will confirm that the first number is valid but the second isn’t:

RegEx to Validate Visa Card Number

A Visa Card number is valid if it satisfies the following conditions:

The string should not contain any special characters, alphabets, or whitespaces. The string should start with 4. The number of characters must be equal to 13 or 16. The old visa cards have 13 characters and the new ones have 16 characters. If the number of characters is equal to 13, the last 12 digits must be a number between 0 to 9. If the number of characters is equal to 16, the last 15 digits must be a number between 0 to 9.

The following regex satisfies the above conditions and you can use it to validate a Visa Card number:

Below is the Python approach to validate a Visa Card number:

Again, output confirms the first number is valid while the second is invalid:

RegEx to Validate American Express Card Number

An American Express card number is valid if it satisfies the following conditions:

The string should not contain any special characters, alphabets, or whitespaces. This number of characters must be equal to 15. The string should start with 34 or 37. The last 13 digits must be a number between 0 to 9.

The following regex satisfies the above conditions and you can use it to validate an American Express Card number:

You can validate an American Express Card number using the following Python code:

Once more, the output confirms the first number is valid, but the second is invalid:

Applications of Regular Expressions

You can use some fairly simple regex to validate common credit card numbers. RegEx is a powerful tool that you can use for data pre-processing, pattern matching, data extraction, lexical analysis, natural language processing, web scraping, and more. You can also use regular expressions in web development to handle the validation of HTML forms.