MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / validate_credit_card_number

Function validate_credit_card_number

strings/credit_card_validator.py:55–95  ·  view source on GitHub ↗

Function to validate the given credit card number. >>> validate_credit_card_number('4111111111111111') 4111111111111111 is a valid credit card number. True >>> validate_credit_card_number('helloworld$') helloworld$ is an invalid credit card number because it has nonnumerical

(credit_card_number: str)

Source from the content-addressed store, hash-verified

53
54
55def validate_credit_card_number(credit_card_number: str) -> bool:
56 """
57 Function to validate the given credit card number.
58 >>> validate_credit_card_number('4111111111111111')
59 4111111111111111 is a valid credit card number.
60 True
61 >>> validate_credit_card_number('helloworld$')
62 helloworld$ is an invalid credit card number because it has nonnumerical characters.
63 False
64 >>> validate_credit_card_number('32323')
65 32323 is an invalid credit card number because of its length.
66 False
67 >>> validate_credit_card_number('32323323233232332323')
68 32323323233232332323 is an invalid credit card number because of its length.
69 False
70 >>> validate_credit_card_number('36111111111111')
71 36111111111111 is an invalid credit card number because of its first two digits.
72 False
73 >>> validate_credit_card_number('41111111111111')
74 41111111111111 is an invalid credit card number because it fails the Luhn check.
75 False
76 """
77 error_message = f"{credit_card_number} is an invalid credit card number because"
78 if not credit_card_number.isdigit():
79 print(f"{error_message} it has nonnumerical characters.")
80 return False
81
82 if not 13 <= len(credit_card_number) <= 16:
83 print(f"{error_message} of its length.")
84 return False
85
86 if not validate_initial_digits(credit_card_number):
87 print(f"{error_message} of its first two digits.")
88 return False
89
90 if not luhn_validation(credit_card_number):
91 print(f"{error_message} it fails the Luhn check.")
92 return False
93
94 print(f"{credit_card_number} is a valid credit card number.")
95 return True
96
97
98if __name__ == "__main__":

Callers 1

Calls 2

validate_initial_digitsFunction · 0.85
luhn_validationFunction · 0.85

Tested by

no test coverage detected