Validate Credit Card Numbers

Python Regex Challenge

Credit Card Validator

 

Credit card numbers are a series of digits assigned to a payment card that is used as a method of financial transaction. These numbers are unique to each card. They are used for making purchases, both in-person and online, as well as for various financial transactions such as bill payments, travel reservations, and more. Additionally, credit card numbers are used for security and verification purposes, helping to authenticate the cardholder and authorize transactions.

Instructions

The goal of this Python challenge is to write a Python function to validate and check credit card numbers. The function should adhere to the following conditions for a credit card number to be considered valid:

1) The credit card number should consist of exactly 16 digits.
2) The credit card number must start with either 3, 7, or 9.
3) The credit card number may contain blank spaces after the fourth, eighth, and twelfth digit.
4) The credit card number should only contain integers and blank spaces; no strings.

To complete this Python regex challenge, please follow these steps:

  1. Import the re module.
  2. Define a Python function called “validate_credit_card” that takes a single parameter, “credit_card_number”.
  3. Implement the necessary logic within the function to validate the credit card number based on the conditions mentioned above.
  4. Return a boolean value of True if the credit card number is valid, and False otherwise.

Please make sure to adhere to the following guidelines:

  • Use Python 3.x for your implementation.
  • Ensure that your function name is “validate_credit_card” and that it takes a single parameter named “credit_card_number”.
  • The function should return a boolean value (True or False) based on the validity of the credit card number.

What will you practice and learn?

  • String manipulation techniques such as removing and checking strings.
  • Working with regular expressions to define and match specific patterns in strings.
  • Implementing conditional logic to validate input based on defined conditions.
  • Understanding the importance of data validation and input verification in programming.
  • Gaining practical experience in building a function to solve a real-world problem, enhancing your problem-solving and programming skills

Visit other real Python challenges to keep working on real-life projects.

Source Code


import re

def validate_credit_card(card_number):
    # Remove any blank spaces from the card number
    card_number = card_number.replace(' ', '')
    
    # Check if the card number has exactly 16 digits
    if not re.match(r'^\d{16}$', card_number):
        return False
    
    # Check if the card number starts with 3, 7, or 9
    if not re.match(r'^[379]', card_number):
        return False
    
    # Check if the blank spaces are at the correct positions
    if not re.match(r'^\d{4}\s?\d{4}\s?\d{4}\s?\d{4}$', card_number):
        return False

Expected outcome

validate_credit_card('3458 56981236 7412')

True

We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for Practice
Register New Account
Shopping cart