How to Validate Credit Card Numbers Using Regular Expressions

A regular expression is a string that forms a search pattern. Regular expressions are mainly used to search, validate, and transform text or strings.


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


When should RegEx be used to validate a credit card number?

Third party services charge for each transaction attempt. Regardless of whether the transaction is successful or not, you have to pay the fees. In such cases, you want to reject clearly invalid credit card numbers. You can quickly validate credit card numbers with regular expressions.

But you can’t fully rely on regular expression validation for your payment function. Card issuers regularly change their card number patterns (introducing new patterns or retiring old ones), so the regex method is not the most robust solution. Although you can try to keep up to date with the map patterns from a source like Wikipedia which is updated frequently.

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

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

The code used in this project is available in a GitHub repository and is free for you to use under the MIT license.

RegEx to validate the Mastercard number

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

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

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

^5[1-5][0-9]{14}|^(222[1-9]|22[3-9]\\d|2[3-6]\\d{2}|27[0-1]\\d|2720)[0-9]{12}$

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

You can use the above regex to validate Mastercard number in any programming language. This is how it works in Python:

import re

def checkMasterCardNo(cardNo):
regex = "^5[1-5][0-9]{14}|^(222[1-9]|22[3-9]\\d|2[3-6]\\d{2}|27[0-1]\\d|2720)[0-9]{12}$"
r = re.compile(regex)

if (re.search(r, cardNo)):
print("Valid")
else:
print("Not Valid")

card1 = "5110762060017101"
checkMasterCardNo(card1)

card2 = "8632458236982734"
checkMasterCardNo(card2)

Running this code will confirm that the first number is valid but the second is not:

RegEx to validate Visa card number

A Visa card number is valid if it meets the following conditions:

  1. The string should not contain any special characters, letters, or spaces.
  2. The string should start with 4.
  3. The number of characters must be 13 or 16. The old Visa cards have 13 characters and the new ones have 16 characters.
    • If the number of characters is 13, the last 12 digits must be a number between 0 and 9.
    • If the number of characters is 16, the last 15 digits must be a number between 0 and 9.

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

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

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

import re

def checkVisaCardNo(cardNo):
regex = "^4[0-9]{12}(?:[0-9]{3})?$"
r = re.compile(regex)

if (re.search(r, cardNo)):
print("Valid")
else:
print("Not Valid")

card1 = "4539890694174109"
checkVisaCardNo(card1)

card2 = "49237429498"
checkVisaCardNo(card2)

Again, the output confirms that 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 meets the following conditions:

  1. The string should not contain any special characters, letters, or spaces.
  2. This number of characters must be equal to 15.
  3. The string should start with 34 or 37.
  4. The last 13 digits must be a number between 0 and 9.

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

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

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

import re

def checkAmericanExpressCardNo(cardNo):
regex = "^3[47][0-9]{13}$"
r = re.compile(regex)

if (re.search(r, cardNo)):
print("Valid")
else:
print("Not Valid")

card1 = "372831730491196"
checkAmericanExpressCardNo(card1)

card2 = "84732593847743042"
checkAmericanExpressCardNo(card2)

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

Applications of regular expressions

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

Leave a Reply

Your email address will not be published. Required fields are marked *