We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Validating Credit Card Numbers
Validating Credit Card Numbers
Sort by
recency
|
473 Discussions
|
Please Login in order to post a comment
Enter your code here. Read input from STDIN. Print output to STDOUT
import re
N = int(input()) credit_num = [] for _ in range(N): cred = input().strip() # Read each line and strip any extra whitespace credit_num.append(cred) # Append the entire line as a single entry
for n in credit_num: if (re.match(r'^[4-6]', n) and len(re.sub(r'[^0-9]', '', n)) == 16 and re.match(r'^[0-9-]+', n) or re.match(r'^\d{16}$', n)) and not re.search(r'(\d)\1{3,}', re.sub(r'[^0-9]', '', n))): print("Valid") else: print("Invalid")
pattern = r"^(?!.*([0-9])(?:-?\1){3})([456]\d{3})-?(\d{4})-?(\d{4})-?(\d{4})$"
Enter your code here. Read input from STDIN. Print output to STDOUT
def check_no_of_digits(p): digits_list = [i for i in p if i.isdigit()] if len(digits_list) == 16: return True return False def check_starting_digits(q): if q[0]=='4' or q[0]=='5' or q[0]=='6': return True return False def check_digits(r): for i in r: if not i.isdigit(): return False return True def check_hyphens(s): if "-" not in s: return True else: l = s.split("-") for i in l: if len(i)!=4: return False return True def consecutive_check(t): lst = [i for i in t if i.isdigit()] #print(lst) for i in range(len(lst) - 3): # Ensure we check till len(lst) - 4 if lst[i] == lst[i+1] == lst[i+2] == lst[i+3]: return True return False n = int(input()) c = 0 for i in range(n): x = input() y = check_no_of_digits(x) z = check_starting_digits(x) a = check_digits(x) b = check_hyphens(x) c = consecutive_check(x) if y and z and a and not c and b: print("Valid") else: print("Invalid")
Fredrick should verify his ABCD Bank credit card by ensuring it starts with 4, 5, or 6, has exactly 16 digits, only contains numbers (or groups separated by hyphens), and doesn’t have four or more consecutive repeating digits. Regex is a great way to catch mistakes quickly. If he ever faces issues with claims or disputes, he might want to check out PCP Claims for possible solutions.
python3