Validating Credit Card Numbers

Sort by

recency

|

487 Discussions

|

  • + 0 comments

    here is my code without regex because i don't know what that is

    `num = int(input())
    i = 0
    credits = [None] * num
    while (i < num):
        credits[i] = input()
        i += 1
    
    for credit in credits:
        if credit[0] not in "456":
            print ("Invalid")
            continue
        count = 0
        digit = False
        is_16_digit = False
        is_consecutive = False
        hyphen_count = 0
        for c in credit:
            if c.isdigit():
                count +=1
                digit = True
            elif c == "-" and count % 4 == 0:
                hyphen_count += 1
                continue
            else:
                digit = False
                break
            if count == 16:
                is_16_digit = True
        credit = credit.replace("-","")
        for c in credit:
            if c+c+c+c in credit:
                is_consecutive = True
                break
        if is_16_digit == False:
            print("Invalid")
            continue
         
        elif digit == False:
            print("Invalid")
            continue
        elif is_consecutive:
            print("Invalid")
            continue
        elif hyphen_count > 3:
            print("Invalid")
            continue
        else:
            print("Valid")
        
        
    

    `

  • + 0 comments
    import re
    pattern = r'([456]\d{15}|[456]\d{3}(-\d{4}){3})'
    for _ in range(int(input())):
        card_no = input()
        if not re.fullmatch(pattern,card_no):
            print("Invalid")
            continue
            
        card_no = card_no.replace("-","")
        
        if re.search(r'(\d)\1{3,}',card_no):
            print("Invalid")
        else:
            print("Valid")
            
        
            
        
    
  • + 0 comments
    import re
    def isvalid(a):
        pattern=r"^[456]\d{15}|[456]\d{3}(\-\d{4}){3}$"
        if not re.match(pattern,a):
            return False
        a=a.replace("-","")
        if re.findall(r"(\d)\1{3,}",a):
            return False
        return True
    
         
    for _ in range(int(input())):
        a=input()
        print("Valid" if isvalid(a) else "Invalid")
    
  • + 0 comments

    my solution

    import re
    
    patern =r'^[4|5|6](\d{15}|\d{3}(-\d{4}){3})'
    duplicate=r'(\d)(-?\1){3}'
    N= int(input())
    for i in range(N):
        card= input()
        if len(card)<=19 and re.match(patern, card):
            if re.search(duplicate, card):
                print("Invalid")
            else:
                print("Valid")
        else:
            print("Invalid")
        
    
  • + 0 comments

    Love this challenge ! It was very fast for me to check the 5 first conditions in regex, but so complex to check the "Not 4 or more consecutive repeated digits" condition.

    For those who come after, I think that a good tip is to create 2 regex patterns. Instead of doing 1 big pattern.

    In my logic, I check all conditions except the first one. Because if already one of them is not valid, I can print "Invalid".

    But if all conditions are valid, then I check the consecutive repeated digit condition.

    For that, I firstly remove "-" if necessary, because it's obviously most complex if not.

    Then I use the regex pattern "([0-9])\1{3,}".

    I didn't know the "back reference" in regex (\1, \2 etc...).

    \1 means : "re-use the last group"

    So ([0-9])\1 means : I capture a digit. And I want this exact same digit after.

    Obviously, by adding {3,} we want that this logic is applied 3 times or more.

    And if this condition is True, it's Invalid, because we don't want to have 4 consecutives repeated digits.

    Note: Be careful to use re.search() and not re.match() for this pattern. Because re.match() only search at the start, whereas re.search() search everywhere in the string.

    Here my code :

    import re
    
    N = int(input())
    
    pattern = r"(^([456])([0-9]{3})-([0-9]{4})-([0-9]{4})-([0-9]{4})$)|(^([456])([0-9]{15})$)"
    # Easier to have a second regex pattern only for the repeated consecutive digit condition
    pattern_repeated_digit = r"([0-9])\1{3,}"
    
    for _ in range(N):
        current_credit_card = input()
    
        if bool(re.match(pattern, current_credit_card)):
            
            # Now that we have check all conditions except repeated digits, we will check this last condition. Before, we have to remove '-'. However, it will be too complex.
            if '-' in current_credit_card:
                current_credit_card = current_credit_card.replace('-', '')
            
            # If there is consecutive repeated digit : Invalid. Else : Valid.
            if bool(re.search(pattern_repeated_digit, current_credit_card)):
                print("Invalid")
            else:
                print("Valid")
        else:
            print("Invalid")