-
Notifications
You must be signed in to change notification settings - Fork 0
/
email validation.py
63 lines (45 loc) · 1.22 KB
/
email validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def has_invalid_characters(string):
valid = "abcdefghijklmnopqrstuvwxyz0123456789."
# your code here
for character in string:
if character not in valid:
return True
return False
def is_valid(email):
if email.count('@') != 1:
return False
prefix,domain = email.split('@')
if len(prefix) < 1:
return False
if email.count('.') != 1:
return False
if has_invalid_characters(prefix) or has_invalid_characters(domain):
return False
domain_parts = domain.split('.')
if len(domain_parts) != 2 or '' in domain_parts:
return False
domain_name,extension = domain.split('.')
if len(domain_name) == 0 or len(extension) == 0:
return False
return True
emails = [
"test@example.com",
"valid@gmail.com",
"invalid@gmail",
"invalid",
"not an email",
"invalid@email",
"!@/",
"test@@example.com",
"test@.com",
"test@site.",
"@example.com",
"an.example@test",
"te#st@example.com",
"test@exam!ple.com"
]
for mail in emails:
if is_valid(mail):
print(mail + ' is valid')
else:
print(mail + ' is invalid')