import re def email_check(email): ''' This function check an email 1. e-mail composed of a name and a domain part, these parts are separated by the symbol " @" ; 2. the domain part is not shorter than 3 characters and no longer than 256, is a set of strings of characters az 0 -9_ - and delimited ; 3. in the domain of each such line between the points can not begin or end with a "-" ; 4. name (up to @ ) does not exceed 128 characters , consists of characters a-z0-9"._- ; 5. The name is not permitted two consecutive terms ; 6. if the name is double quotation marks ", they must be paired ; 7. in the name can meet the characters "!,:" but only in the double quotes . ''' email_temp = email.split("@") email_name = email_temp[0] email_domain = email_temp[1] if '..' in email_domain or '..' in email_name: raise ValueError('Error! You entered two periods in a row.') elif len(email_domain) < 3 or len(email_domain) > 256: raise ValueError('Error! Domain name must be from 3 to 255 characters.') elif len(email_name) > 128: raise ValueError('Error! The name of Email address must be less than 128 characters.') if '"' in email_name: if email_name.count('"')%2 != 0: raise ValueError('Error! Use double quotes.') else: email_name_parts = email_name.split('"') counter = 0 parts_between_quotes = [] parts_without_quotes = [] while counter < len(email_name_parts): if counter%2 != 0: parts_between_quotes.append(email_name_parts[counter]) else: parts_without_quotes.append(email_name_parts[counter]) counter+=1 for part in parts_between_quotes: chars = re.search('[a-zA-Z0-9!,:_-]+',part) if chars.group() != part: raise ValueError('Error! Part of the Email name in quotes must consist of the characters a-zA-Z0-9!,:_-') for part in parts_without_quotes: chars = re.search('[a-zA-Z0-9_-]+',part) if chars.group() != part: raise ValueError('Error! Part of the Email name without quotes must consist of the characters a-z0-9._-') else: chars = re.search('[a-zA-Z0-9"._-]+',email_name) if chars.group() != email_name: raise ValueError('Error! The name of Email address may consist of the characters a-z0-9"._-') domain_parts = email_domain.split(".") for part in domain_parts: if part.startswith('-') or part.endswith('-'): raise ValueError("Error! Domain name and domain area can not begin or end with '-'.") chars = re.search('[a-zA-Z0-9_-]+',part) if chars.group() != part: raise ValueError('Error! Domain name and domain zone must consist of the characters a-z0-9_-') return "Email "+email+" is correct!" print email_check('wlionheart@gmail.com')
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question