diff --git a/Robust Email Validator/Readme.me b/Robust Email Validator/Readme.me new file mode 100644 index 0000000..e69de29 diff --git a/Robust Email Validator/main.py b/Robust Email Validator/main.py new file mode 100644 index 0000000..5843e33 --- /dev/null +++ b/Robust Email Validator/main.py @@ -0,0 +1,47 @@ +def validate_email(email: str) -> bool: + email = email.strip().lower() + + if '@' not in email or "." not in email: + print("You must include \"@\" and \".\" in your email!") + return False + + try: + email, domain = email.split('@', 1) + except ValueError: + print("Email must contain exactly one \"@\" symbol") + return False + if not email: + print("You have nothing before the '@'!") + return False + + if "." not in domain: + print("You are mising the '.'!") + return False + + if domain.startswith('.') or domain.endswith('.'): + print("You placed the period at the incorrect location!") + return False + + split_domain = domain.split(".") + if any(not part for part in split_domain): + return False + + if len(split_domain[-1]) < 2: + return False + + return True + + +test_emails = [ + "john.doe@example.com", + " Jane@domain.org ", + "noatsymbol.com", + "@missinglocal.com", + "missingdot@domain", + "dot..error@domain.com", + "wrongend@domain.", + "short@domain.c" +] + +for email in test_emails: + print(f"{email} -> {validate_email(email)}") \ No newline at end of file