Validating Email Addresses With a Filter

Sort by

recency

|

620 Discussions

|

  • + 0 comments
    import string
    
    def fun(s):
        try:
            username, website_extention = s.split("@")
            website, extention = website_extention.split(".")
        except ValueError:
            return False
        
        # Fix: reject empty parts
        if not username or not website or not extention:
            return False
    
        allowed_username = string.ascii_letters + string.digits + "_-"
        allowed_website = string.ascii_letters + string.digits
    
        for c in username:
            if c not in allowed_username:
                return False
    
        for c in website:
            if c not in allowed_website:
                return False
    
        if not extention.isalpha() or len(extention) > 3:
            return False
    
        return True
    
  • + 0 comments
    import re
    
    EMAIL_REGEX = re.compile("^[a-zA-Z0-9_-]+@[a-zA-Z0-9]+\.[a-zA-Z]{1,3}$")
    
    def fun(s):
        return EMAIL_REGEX.match(s) is not None
    

    Pre-compile the pattern and match the regex so that it avoids compilation everytime

  • + 0 comments
    import re
    
    def fun(s):
        return re.match(r"^[\w-]+@[a-zA-Z0-9]+\.[a-zA-Z]{1,3}$", s) is not None
    
  • + 0 comments
    def fun(s):
        try:
            n, m = s.split("@")
            w, e = m.split(".", 1)
        except ValueError:
            return False
        if not n or not w or not e:
            return False
        sp= "_-"
        def cond1(n):
            for i in n:
                if not (i.isupper() or i.islower() or i.isdigit() or i in sp):
                    return False
            return True
            
        def cond2(w):
            for i in w:
                if not (i.isupper() or i.islower() or i.isdigit()):
                    return False
            return True
            
            
        def cond3(e):
            if len(e) > 3:
                return False
            for i in e:
                if not (i.isupper() or i.islower()):
                    return False
            return True
        result = all([cond1(n),cond2(w),cond3(e)])
        return result
                
    
  • + 0 comments

    using python functionals (No regex)

    extname = s[compsep+1:]
    
    if not (username and compname and extname):
        return False
    if not (username == ''.join(list(filter(lambda c: c.isalnum() or c in '_,-', username)))):
        return False
    if not (compname == ''.join(list(filter(lambda c: c.isalnum(), compname)))):
        return False
    if not (extname == ''.join(list(filter(lambda c: c.isalnum(), extname))) and len(extname) <= 3):
        return False
    return True