How do you detect Credit card type based on number?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Detecting Credit Card Type Based on Number

Credit card numbers follow specific patterns that can be used to identify the card type. Each type of credit card has a distinct prefix (also known as the Issuer Identification Number or IIN) and a specific length. Here’s how you can detect the credit card type based on the number.

Common Credit Card Types and Their Patterns

  1. Visa

    • Starts with: 4
    • Length: 13, 16, 19
  2. MasterCard

    • Starts with: 51 through 55 or 2221 through 2720
    • Length: 16
  3. American Express

    • Starts with: 34 or 37
    • Length: 15
  4. Discover

    • Starts with: 6011, 622126 through 622925, 644 through 649, 65
    • Length: 16, 19
  5. Diners Club

    • Starts with: 300 through 305, 36, 38, 39
    • Length: 14
  6. JCB

    • Starts with: 3528 through 3589
    • Length: 16
  7. Maestro

    • Starts with: 50, 56 through 69
    • Length: 12 to 19

Implementation in Python

Here is a Python function to detect the credit card type based on the number:

import re def get_credit_card_type(number): number = str(number) card_types = { "Visa": r"^4\d{12}(\d{3})?(\d{3})?$", "MasterCard": r"^(5[1-5]\d{4}|222[1-9]|22[3-9]\d{3}|2[3-6]\d{4}|27[01]\d{3}|2720)\d{10}$", "American Express": r"^3[47]\d{13}$", "Discover": r"^6(?:011|5\d{2}|4[4-9]\d|22[1-9]\d{1,2})\d{12}$", "Diners Club": r"^3(?:0[0-5]|[68]\d)\d{11}$", "JCB": r"^35(?:2[89]|[3-8]\d)\d{12}$", "Maestro": r"^(50|5[6-9]|6[0-9])\d{10,17}$" } for card_type, pattern in card_types.items(): if re.match(pattern, number): return card_type return "Unknown" # Example usage print(get_credit_card_type("4111111111111111")) # Output: Visa print(get_credit_card_type("5500000000000004")) # Output: MasterCard print(get_credit_card_type("340000000000009")) # Output: American Express print(get_credit_card_type("6011000000000004")) # Output: Discover print(get_credit_card_type("30569309025904")) # Output: Diners Club print(get_credit_card_type("3530111333300000")) # Output: JCB print(get_credit_card_type("6759649826438453")) # Output: Maestro

Explanation

  1. Regex Patterns: The regular expressions (regex) for each card type are constructed based on their known prefixes and lengths.
  2. Matching: The re.match function checks if the credit card number matches any of the card type patterns.
  3. Return Type: The function returns the card type if a match is found, otherwise it returns "Unknown".

Summary

  • Visa: Starts with 4, length 13, 16, or 19.
  • MasterCard: Starts with 51-55 or 2221-2720, length 16.
  • American Express: Starts with 34 or 37, length 15.
  • Discover: Starts with 6011, 622126-622925, 644-649, 65, length 16 or 19.
  • Diners Club: Starts with 300-305, 36, 38, 39, length 14.
  • JCB: Starts with 3528-3589, length 16.
  • Maestro: Starts with 50, 56-69, length 12-19.

By using regex patterns tailored to the specific prefixes and lengths of credit card numbers, you can reliably determine the type of credit card based on its number. For more in-depth knowledge and practical examples on programming concepts, consider exploring Grokking the Coding Interview on DesignGurus.io, which provides comprehensive courses on essential coding and interview techniques.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Why is PayPal cutting jobs?
Why should we choose you?
How many days does Amazon take to give interview results?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking Advanced Coding Patterns for Interviews
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.