Validating Credit Card Numbers

Sort by

recency

|

475 Discussions

|

  • + 0 comments
    t = int(input())
    p1 = r'^[456]'
    p2 = r'^.{16,19}$'
    p3 = r'\d{4}-\d{4}-\d{4}-\d{4}'
    p4 = r'[^@_!#$%^&*()<>?/\|}{~:]'
    p5 = r'(\d)(-\1+)'
    p6 = r'(\d)\1{3,}'
    
    for i in range(t):
        number = input()
        c1 = re.match(p1,number) 
        c2 = re.match(p2,number)
        c3= re.search(p3,number)
        c4 = re.match(p4,number)
        c5 = re.search(p5,number)
        c6 = re.search(p6,number)
        #print(c1,c2,c3,c4,c5,c6)
        if '-' in number:
            if c1 != None and c2 != None and c3 != None and c4 != None and c6 == None and c5 == None:
                print('Valid')
            else:
                print('Invalid')
        else:
            if c1 != None and c2 != None and c4 != None:
                print('Valid')
            else:
                print('Invalid')
    
  • + 0 comments
    import re
    pat12345 = r'^[456]\d{3}-?\d{4}-?\d{4}-?\d{4}$'
    pat6 = r'(\d)\1{3,}'
    
    for i in range(int(input())):
        a = input()
        a2 = re.sub(r'-',"", a)
        
        b1 = re.match(pat12345, a) != None
        b2 = re.search(pat6, a2)   == None
        
        print('Valid' if all([b1, b2]) else 'Invalid')
    
  • + 0 comments

    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-]+Undefined control sequence \d', 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")

  • + 0 comments

    pattern = r"^(?!.*([0-9])(?:-?\1){3})([456]\d{3})-?(\d{4})-?(\d{4})-?(\d{4})$"

  • + 0 comments

    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")