Skip to content

Enhanced email address validator #330

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Address Validator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
48 changes: 37 additions & 11 deletions Address Validator/AddressValidator.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
import time


'''
Fixes:
- Now checks number of '@' symbols
- Now checks if characters come before and after the '@' symbol
- Now checks position of '.'
'''

def addressVal(address):
dot = address.find(".")
at = address.find("@")
if (dot == -1):
print("Invalid")
elif (at == -1):
print("Invalid")
else:
print("Valid")
"""
Validates an email address.
Ensures only one '@' is present, the '@' is the not the first char,
there is atleast one '.' after the '@', and the '.' is not the last
char in the email.

Args:
address (str): The email to be validated.
Returns:
str: "Valid" if the email is valid, an invalid message otherwise.
"""
if '@' not in address or address.count('@') != 1:
return "Invalid: Email must contain exactly one '@' symbol!"

at_pos = address.find('@')
dot_pos = address.find('.', at_pos)

if at_pos == 0 or at_pos == len(address) - 1:
return "Invalid: Characters must appear before or after the '@'!"

if dot_pos == -1 or dot_pos == len(address) - 1 or dot_pos < at_pos + 2:
return "Invalid: Dot must come after the '@'!"

return "Valid"


print("This program will decide if your input is a valid email address")
while(True):
print("A valid email address needs an '@' symbol and a '.'")
x = input("Input your email address:")

addressVal(x)
result = addressVal(x)
print(result)