Regex to validate credit card number

The purpose of this tutorial is to show how to use a Bash shell script or Python programming code to validate a credit card number on a Linux system.

Check out the example scripts below to validate credit card numbers from the Linux command line.

In this tutorial you will learn:

  • How to use Bash to validate credit card number
  • How to use Python to validate credit card number
Regex to validate credit card number
Regex to validate credit card number
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux system
Software Bash, Python
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Regex to validate credit card number – Bash and Python examples




As you may have guessed, many different programming langauges could be used to validate a credit card number in Linux. A script just needs to use regular expression to make sure that 16 digits are present (along with spaces or dashes in some cases). In other words, numbers like:

  • 1234 5678 1234 5678
  • 1234567812345678
  • 1234-5678-1234-5678

Two of the most popular tools for the task on Linux are the Bash shell and Python programming language. Both of these are capable of checking credit card numbers with only a few short lines of code. Use your preferred method below and copy our script examples to your own system to start validating credit card numbers.

Bash example to validate credit card numbers

Bash script:

#!/bin/bash

# regexp to get a valid credit card number

echo $1 | grep -qE '^([0-9]{4}[- ]?){3}[0-9]{4}$'

if [ $? -eq 0 ]; then
	echo "$1 is a valid credit card number."
else
	echo "$1 is an invalid credit card number."
fi

Execution of Bash script:

$ chmod +x validate_credit_card_number.sh

$ ./validate_credit_card_number.sh "1234 5678 1234 5678"
1234 5678 1234 5678 is a valid credit card number.

$ ./validate_credit_card_number.sh 1234567812345678
1234567812345678 is a valid credit card number.

$ ./validate_credit_card_number.sh 1234-5678-1234-5678
1234-5678-1234-5678 is a valid credit card number.

$ ./validate_credit_card_number.sh 1234-5678-1234-56786
1234-5678-1234-56786 is an invalid credit card number.

$ ./validate_credit_card_number.sh 1234-55678-1234-5678
1234-55678-1234-5678 is an invalid credit card number.

Python example to validate credit card numbers

Python script:

import re
cc_list=['1234 5678 1234 5678',
'1234567812345678',
'1234-5678-1234-5678',
'1234-5678-1234-56786',
'1234-55678-1234-5678']
pattern = '^([0-9]{4}[- ]?){3}[0-9]{4}$'
for eachnumber in cc_list:
    result = re.match(pattern, eachnumber)
    if result:
        print(eachnumber+" is a valid credit card number.")
    else:
        print(eachnumber+" is an invalid credit card number.")

Execution of Python script:

$ python3 validate_credit_card_number.py

1234 5678 1234 5678 is a valid credit card number.

1234567812345678 is a valid credit card number.

1234-5678-1234-5678 is a valid credit card number.

1234-5678-1234-56786 is an invalid credit card number.

1234-55678-1234-5678 is an invalid credit card number.

Closing Thoughts




In this tutorial, we learned how to use a Bash or Python script to validate credit card numbers on Linux. Since only regular expression is used, this ends up being a pretty simple job for either programming language. And, of course, many other programming languages are capable of the task.



Comments and Discussions
Linux Forum