Python program to validate a credit card number

In this post, we are going to use a Python program to validate a credit card number by assuming a few conditions using the regex module in Python. But what is a regex, well ‘regex’ stands for regular expressions in simple words this module allows us to find patterns in a given string or find all strings of a given pattern which will be very interesting. Let us proceed ahead and see how it works and what are its uses.

conditions to validate a credit card number

 

  1. It must contain exactly 16 digits.
  2. It should only contain 0-9 digits.
  3. It must start with either 9 or 7 or 3.
  4. It may have digits in a group of 4 with a separator (-).
  5. It must not contain any other symbols such as _ or space(‘ ‘).

These conditions are just for our convenience’s sake only just for understanding purposes.
First, let us see some examples of valid and invalid credit card numbers with our conditions applied to it for a python program to validate a given credit card number.

  • 92136258797157867 #17 digits in card number -> Invalid
  • 7123456789123456 ->Valid
  • 3123-7754-9978-2343 ->Valid
  • 4997-5624-9832-2211 Starting with digit 4 -> Invalid
  • 9675 – 7756-8864-9075 contains space in between ->Invalid

Validate Email in Python

How to validate a credit card number in Python

import re
sample_list=['3123456789123456','9123-4567-8912-3456','5123456789123456','7123 - 4567-8912 -3456','44244x4424442444','0525362587961578']
pattern = '^[973][0-9]{15}|[973][0-9]{3}-[0-9]{4}-[0-9]{4}-[0-9]{4}$'
for eachnumber in sample_list:
    result = re.match(pattern, eachnumber)
    if result:
        print(eachnumber+"->"+"Valid card")
    else:
        print(eachnumber+"->"+"Invalid card")
output:

3123456789123456->Valid card

9123-4567-8912-3456->Valid card

5123456789123456->Invalid card

7123 - 4567-8912 -3456->Invalid card

44244x4424442444->Invalid card

0525362587961578->Invalid card


Understanding validation of a credit card

 

Here import re is a statement in python to import the regular expression module to make use of its functionalities.

we created a sample list that consists of sample card numbers to detect whether they are valid or not.

the pattern here is the base for us to find what type of card numbers can exist or not ^ this expression will match the start of the string.

case-1(it should be plain 16 digits number) :  [973] represents that the starting of the string should be either 9 or 7 or 3. And [0-9] says that a number that can be from 0-9 [0-9]{10} indicates that any number 0-9 can occur 10 times so in our code it is 15 times as the first number must be of the three numbers[973].

case-2(it is 16 digits number as well can contain -):    [973]represents the starting of string should be 9 or 7 or 3 and followed by [0-9]{3} as after every four digits a hyphen should be there followed by again the same process [0-9]{4} 0-9 any four digits again -.

so both case1 and case2 are acceptable hence we can consider both by using | (or) sign either case1 or
case2 is possible.

Finally, there is a for loop which iterates through each number name (eachnumber) in the list if the pattern.
matches then it will print valid else invalid.

basic builtin functions in python

Leave a Reply

Your email address will not be published.