Validating Credit Card Numbers

Sort by

recency

|

464 Discussions

|

  • + 0 comments
    import re
    p = r'^[456]\d{3}(-?\d{4}){3}$'
    for _ in range(int(input())):
        s  = input()
        if re.match(p, s):
            s = s.replace('-', '')
            if not re.search(r'(.)\1{3}', s):
                print("Valid")
            else:
                print("Invalid")
        else:
            print("Invalid")
    
  • + 0 comments
    import re
    
    n = int(input())
    
    pattern = re.compile(r'^[4-6](\d{3})-?(\d{4})-?(\d{4})-?(\d{4})$')
    pattern_duplicate = re.compile(r'(\d)\1{3}')
    
    for x in range(n):
        card = input()
        if bool(pattern.match(card)):
            valid_card = re.sub(r'-', "", card)
            if not bool(pattern_duplicate.search(valid_card)):
                print("Valid")
            else:
                print("Invalid")
            
        else:
            print("Invalid")
    
  • + 0 comments
    import re
    # Function to validate credit card numbers
    def validate_credit_card(card_number):	
        pattern = r'^[456]\d{3}(-?\d{4}){3}$'    # Regex pattern for a valid card number
    # Check if the card matches the pattern and has no consecutive repeated digits
        if re.match(pattern, card_number) and not re.search(r'(\d)\1{3,}', card_number.replace('-', '')):
            return "Valid"
        else:
            return "Invalid"
    no_of_cards = int(input())
    card_numbers = [input() for _ in range(no_of_cards)]
    
    # Applying the function to each card number
    results = [validate_credit_card(card) for card in card_numbers]
    for result in results:
        print(result)
    
  • + 0 comments

    Test case 3 is failed and i am not getting the problem where is : This is my code:

    import re

    def check(s): #1check if no of digits are 16 or not after removing - if len(s.replace("-",""))==16: for i in s: if i=="-" or i.isdigit(): continue else: return "Invalid"

        pattern=r"[456]\d{3}(-?\d{4}){3}"
        s_new=s.replace('-',"")
        c=1
    
        #checking repetetion of same digit equal to or more than 4
        for i in range(len(s_new)-1):
            if "-" in s_new:
                match=re.finditer(r"-",s_new)
                index=[x.start() for x in match]
                if index==[3,7,11]:
                    pass
                else:
                    return "Invalid"
    
            if s_new[i]==s_new[i+1]:
                c+=1
                if c==4:
                    return "Invalid"
                continue
            else:
                c=1
        if c==1:
            if re.match(pattern,s):
                return "Valid"
            else:
                return "Invalid"
    
    else:
        return "Invalid"
    

    n=int(input()) for _ in range(n): s=input() result=check(s) print(result)

  • + 0 comments

    import re

    def is_valid(crd_no):

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

    return bool(re.match(pattern, crd_no))

    n = int(input())

    crd_no = [input() for _ in range(n)]

    for card in crd_no:

    result = is_valid(card) if result:

    print("Valid")
    

    lse: print("Invalid")