Chapter 43: Credit Card Validatorยถ

๐Ÿš€ Open Notebookยถ

Open In Colab Open In Kaggle

๐Ÿ“บ Video Tutorialยถ

Watch on YouTube

Credit card validator in Python ๐Ÿ’ณ (8:49)

๐Ÿ“š What Youโ€™ll Learnยถ

Implement the Luhn algorithm to validate credit card numbers - a real-world application of mathematical algorithms!

๐ŸŽฏ Learning Objectivesยถ

  • Understand the Luhn algorithm (mod 10 algorithm)

  • Convert strings to lists of integers

  • Implement mathematical validation logic

  • Reverse and manipulate lists

  • Create practical validation tools

๐Ÿ“– Concept Explanationยถ

The Luhn Algorithmยถ

The Luhn algorithm (also called mod 10 algorithm) is used to validate credit card numbers, IMEI numbers, and other identification numbers.

How It Works:ยถ

  1. Remove the last digit (check digit)

  2. Reverse the remaining digits

  3. Double every second digit

  4. If doubled digit > 9, subtract 9

  5. Sum all digits

  6. Add the check digit

  7. If total % 10 == 0, the number is valid

Example:ยถ

Credit card: 4532015112830366

  1. Check digit: 6

  2. Remaining: 453201511283036

  3. Reversed: 630382115102354

  4. Double every 2nd: 6,6,0,6,8,4,1,2,5,2,0,4,3,10,5,8

  5. Subtract 9 if > 9: 6,6,0,6,8,4,1,2,5,2,0,4,3,1,5,8

  6. Sum: 61

  7. Add check digit: 61 + 6 = 67

  8. 67 % 10 = 7 โ‰  0, so invalid (this is just an example)

๐Ÿ’ก Examplesยถ

Basic Validatorยถ

def validate_card(card_number):
    # Remove spaces and convert to string
    card_number = card_number.replace(" ", "")
    
    # Check if all characters are digits
    if not card_number.isdigit():
        return False
    
    # Remove last digit (check digit)
    check_digit = int(card_number[-1])
    digits = [int(d) for d in card_number[:-1]]
    
    # Reverse the digits
    digits.reverse()
    
    # Double every second digit
    for i in range(1, len(digits), 2):
        digits[i] *= 2
        if digits[i] > 9:
            digits[i] -= 9
    
    # Sum all digits
    total = sum(digits) + check_digit
    
    # Check if divisible by 10
    return total % 10 == 0

# Test
card = "4532015112830366"
if validate_card(card):
    print("โœ… Valid card number")
else:
    print("โŒ Invalid card number")

Interactive Validatorยถ

def validate_card(card_number):
    # Remove spaces and hyphens
    card_number = card_number.replace(" ", "").replace("-", "")
    
    # Validation checks
    if not card_number.isdigit():
        return False, "Card number must contain only digits"
    
    if len(card_number) < 13 or len(card_number) > 19:
        return False, "Card number must be between 13 and 19 digits"
    
    # Luhn algorithm
    check_digit = int(card_number[-1])
    digits = [int(d) for d in card_number[:-1]]
    digits.reverse()
    
    for i in range(1, len(digits), 2):
        digits[i] *= 2
        if digits[i] > 9:
            digits[i] -= 9
    
    total = sum(digits) + check_digit
    
    if total % 10 == 0:
        return True, "Valid card number"
    else:
        return False, "Invalid card number"

# Main program
print("๐Ÿ’ณ Credit Card Validator")
print("-" * 30)
card = input("Enter card number: ")

is_valid, message = validate_card(card)

if is_valid:
    print(f"โœ… {message}")
else:
    print(f"โŒ {message}")

With Card Type Detectionยถ

def get_card_type(card_number):
    """Identify the card type based on the first digits"""
    if card_number.startswith('4'):
        return "Visa"
    elif card_number.startswith('5'):
        return "Mastercard"
    elif card_number.startswith('37'):
        return "American Express"
    elif card_number.startswith('6'):
        return "Discover"
    else:
        return "Unknown"

def validate_card(card_number):
    # Remove spaces and hyphens
    card_number = card_number.replace(" ", "").replace("-", "")
    
    if not card_number.isdigit():
        return False, "Invalid format", None
    
    if len(card_number) < 13 or len(card_number) > 19:
        return False, "Invalid length", None
    
    # Luhn algorithm
    check_digit = int(card_number[-1])
    digits = [int(d) for d in card_number[:-1]]
    digits.reverse()
    
    for i in range(1, len(digits), 2):
        digits[i] *= 2
        if digits[i] > 9:
            digits[i] -= 9
    
    total = sum(digits) + check_digit
    card_type = get_card_type(card_number)
    
    if total % 10 == 0:
        return True, "Valid", card_type
    else:
        return False, "Invalid checksum", card_type

# Test
print("๐Ÿ’ณ Credit Card Validator")
print("-" * 40)
card = input("Enter card number: ")

is_valid, message, card_type = validate_card(card)

if is_valid:
    print(f"โœ… Valid {card_type} card")
else:
    print(f"โŒ {message}")
    if card_type != "Unknown":
        print(f"   Appears to be {card_type} format")

โœ๏ธ Practice Exercisesยถ

Exercise 1: Basic Validatorยถ

Implement a simple Luhn algorithm validator that:

  1. Takes a card number as input

  2. Validates using Luhn algorithm

  3. Prints valid or invalid

Exercise 2: Format Checkerยถ

Add validation for:

  • Only digits (or spaces/hyphens)

  • Correct length (13-19 digits)

  • No letters or special characters

Exercise 3: Batch Validatorยถ

Create a program that validates multiple cards from a file:

cards = [
    "4532015112830366",
    "6011514433546201",
    "371449635398431",
    "0000000000000000"
]

for card in cards:
    is_valid, message, card_type = validate_card(card)
    status = "โœ…" if is_valid else "โŒ"
    print(f"{status} {card}: {card_type} - {message}")

๐Ÿ” Common Mistakesยถ

1. Not Reversing Before Doublingยถ

# โŒ Wrong - doubles wrong digits
for i in range(1, len(digits), 2):
    digits[i] *= 2

# โœ… Correct - reverse first
digits.reverse()
for i in range(1, len(digits), 2):
    digits[i] *= 2

2. Forgetting to Subtract 9ยถ

# โŒ Wrong - doesn't handle > 9
digits[i] *= 2

# โœ… Correct
digits[i] *= 2
if digits[i] > 9:
    digits[i] -= 9

3. Including Check Digit in Sum Before Addingยถ

# โŒ Wrong - check digit doubled
digits = [int(d) for d in card_number]

# โœ… Correct - exclude check digit
check_digit = int(card_number[-1])
digits = [int(d) for d in card_number[:-1]]

๐ŸŽฎ Real-World Applicationsยถ

  1. E-commerce: Validate cards before processing

  2. Payment Gateways: Pre-validation before API calls

  3. Form Validation: Client-side card number checking

  4. Testing: Generate valid test card numbers

  5. Security: Quick check before expensive validation

๐Ÿš€ Challenge Projectsยถ

Challenge 1: Card Generatorยถ

Create valid test card numbers using the Luhn algorithm:

def generate_card(prefix, length):
    # Generate random digits
    # Calculate check digit using Luhn
    # Return valid card number
    pass

Challenge 2: Masked Displayยถ

Show card number with masking:

  • Input: 4532015112830366

  • Output: 4532 **** **** 0366

Challenge 3: Expiry Date Validatorยถ

Extend to validate:

  • Card number (Luhn)

  • Expiry date (MM/YY format, not expired)

  • CVV (3-4 digits)

๐Ÿ“ Key Takeawaysยถ

  • Luhn algorithm validates identification numbers

  • Reverse digits before doubling

  • Double every second digit (after reversing)

  • Subtract 9 if doubled digit > 9

  • Sum must be divisible by 10

  • Always validate format before applying algorithm

  • Card type can be identified from first digits

๐ŸŽ“ Key Takeaways from Videoยถ

  1. Lists store multiple items in a single variable

  2. Use loops to repeat actions

  3. Use if-elif-else for conditional logic

๐Ÿ’ก These points cover the main concepts from the video tutorial to help reinforce your learning.

๐Ÿ”— Next Chapterยถ

Continue to Chapter 44: Banking Program to build a complete banking application!


Security Note: This validates the format only. Real validation requires checking with the card issuer. Never store card numbers in plain text!