Validating Credit Card Numbers

  • + 0 comments
    ''''
    A valid credit card from ABCD Bank has the following characteristics:
    â–º It must start with a 4, 5 or 6.                                 ---- ^[456]
    â–º It must contain exactly 16 digits.                              ----
    â–º It must only consist of digits (0-9).
                    ---- \d{3}-?\d{4}-?\d{4}-?\d{4} - after starting 4,5,6 check if
                    ---- there are 3 digits "d{3}", followed by an optional - ("-?")
                    ---- there are 4 digits "d{4}", followed by an optional - ("-?") .. repeat for 16 digits
    â–º It may have digits in groups of 4, separated by one hyphen "-". ----("-?") optional hyphen
    â–º It must NOT use any other separator like ' ' , '_', etc.        ----("-?") only optional separator, others will fail
    â–º It must NOT have or more consecutive repeated digits.           ----r'(.)\1{3}'
                        ---- (.) match any character and group it
                        ---- \1{3} matches exactly 3 more occurances of the character
    '''
    import re
    
    
    for z in range(int(input())):
        x=input()
        print('\n',x, len(x), type(x))
        pattern = r'^[456]\d{3}-?\d{4}-?\d{4}-?\d{4}$'
        pattern2 = r'(.)\1{3}'
        x2 = re.sub('-','',x)
    
        print(x2)
    
        if re.fullmatch(pattern,x) and not re.findall(pattern2,x2):
            print("Valid")
        else:
            print("Invalid")