Validating Credit Card Numbers

Sort by

recency

|

469 Discussions

|

  • + 0 comments

    python3

    import re 
    
    check1 = r'(?=(.)(-?\1){3,}.*)' # repeat
    validcredit = r'^(4|5|6)[0-9]{3}(-?[0-9]{4}){3}$'
    
    check1 = re.compile(check1)
    check2 = re.compile(validcredit)
    
    t = int(input())
    
    for i in range(t):
        num = input()
        if check1.search(num):
            print("Invalid")
        elif check2.match(num):
            print("Valid")
        else:
            print("Invalid")
    
  • + 0 comments
    import sys
    import re
    
    def is_valid_format(card_number):
        """Check if the card number has the correct format."""
        format_pattern = r"^[456]\d{3}([-]?\d{4}){3}$"
        return bool(re.match(format_pattern, card_number))
    
    def has_repeated_digits(card_number):
        """Check if the card number contains four consecutive repeated digits."""
        cleaned_number = ''.join(card_number.split('-')) 
        return bool(re.search(r"(.)\1{3}", cleaned_number))
    
    def validate_card_numbers(card_numbers):
        """Validate a list of card numbers and return the results."""
        results = []
        for card in card_numbers:
            if is_valid_format(card) and not has_repeated_digits(card):
                results.append("Valid")
            else:
                results.append("Invalid")
        return results
    
    if __name__ == "__main__":
        text = sys.stdin.read().splitlines()[1:] 
        results = validate_card_numbers(text)
        print("\n".join(results))
    
  • + 0 comments
    import re
    
    def is_valid(card_nr: str) -> bool:
        if not re.match(r'[456]\d{3}(-?\d{4}){3}$', card_nr):
            return False
        return not any(re.finditer(r'(.)\1{3}', card_nr.replace("-", "")))
        
    
    if __name__ == "__main__":
        for _ in range(int(input())):
            print("Valid" if is_valid(input()) else "Invalid")
    
  • + 1 comment
    import sys, re
    
    text = sys.stdin.read().splitlines()[1:]
    format_pattern = r"^[456]\d{3}([-]?\d{4}){3}$"
    match = []
    
    for line in text:
        format_match = re.search(format_pattern, line)
    
        if format_match != None:
            occurence_match = re.search(r"(.)\1{3}", ''.join(re.split('-', line)))
            
            if occurence_match == None:
                match += ['Valid']
            else:
                match += ['Invalid']
        else:
            match += ['Invalid']
    
    for _ in match:
        print(_)
    
  • + 0 comments
    import re
    
    n = int(input())
    pattern = r'^(?!.*(.)(-?\1){3})[456]\d{3}(-?\d{4}){3}$'
    
    for _ in range(n):
        result = re.match(pattern, input())
        
        print('Valid' if result else 'Invalid')