-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.py
244 lines (139 loc) · 5.5 KB
/
validate.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
## Regular Expressions are regexes
## Validate the email address of a user
# email = input("What's your email? ").strip() # the strip() deleted spaces or in other words 'whitespace'
# if "@" in email:
# print("Valid")
# else:
# print("Invalid")
# ## However, the above only validates if a '@' is inputted
# ## Let's add another boolean feature, in this case '.'
# email = input("What's your email? ").strip()
# if "@" in email and "." in email:
# print("Valid")
# else:
# print("Invalid")
# ## However, code above is still not so good
# email = input("What's your email? ").strip()
# username, domain = email.split("@")
# if username and "." in domain:
# print("Valid")
# else:
# print("Invalid")
# ## Furthermore, we could be more percise by having the code only validate if the email ends with '.edu'
# email = input("What's your email? ").strip()
# username, domain = email.split("@")
# if username and domain.endswith(".edu"):
# print("Valid")
# else:
# print("Invalid")
# ## However, if user does not include domain such as @'gmail' it will still valid the email input
## Using 're.search'
# import re
# email = input("What's your email? ").strip()
# username, domain = email.split("@")
# if re.search("@", email):
# print("Valid")
# else:
# print("Invalid")
# ## This is no different to our codes above, as this only looking for a "@" that user may input
# ## To further the 're' approach
# import re
# email = input("What's your email? ").strip()
# username, domain = email.split("@")
# if re.search(".+@.+", email):
# print("Valid")
# else:
# print("Invalid")
# ## Furthermore to code above
# import re
# email = input("What's your email? ").strip()
# username, domain = email.split("@")
# if re.search(".+@.+.edu", email): # this is not so great because we could use any character where we want the dot before edu as in '.edu'
# print("Valid")
# else:
# print("Invalid")
# ## Improve code above
# import re
# email = input("What's your email? ").strip()
# username, domain = email.split("@")
# if re.search(r".+@.+\.edu", email): # using the 'r' is for the raw string to code to understand to print code as is!
# print("Valid")
# else:
# print("Invalid")
# ## However, this code is still not so good because a user can input multiple '@'s such as rob@@@ucsd.edu and code would consider as 'Valid'
# ## similarly, users could add more characters to the left and right and consider as 'Valid'
# ## Using '^' and '$' to prevent users from inputting characters before and after their actual email
# ### '^': matches the start of the string
# ### '$': matches the end of the string just before the newline at the end of the string
# import re
# email = input("What's your email? ").strip()
# username, domain = email.split("@")
# if re.search(r"^.+@.+\.edu$", email):
# print("Valid")
# else:
# print("Invalid")
# ## However, the code above still allows for multiple "@'s" such as rob@@ucsd.edu
# ## Addressing the prevention of code validating multiple '@'s' from user by using '[]'
# ## the '[]' is used an except, specifies a set of characters
# ## [^] complementing the set
# import re
# email = input("What's your email? ").strip()
# username, domain = email.split("@")
# if re.search(r"^[^@]+@[^@]+\.edu$", email): # the [^@] is saying everything EXCEPT the '@' sign!
# print("Valid")
# else:
# print("Invalid")
# ## To specify a range of letters for an email validation?
# import re
# email = input("What's your email? ").strip()
# username, domain = email.split("@")
# if re.search(r"^[a-zA-Z0-9_]+@[a-zA-Z0-9_]+\.edu$", email):
# print("Valid")
# else:
# print("Invalid")
## However, you can simplify the '[a-zA-Z0-9_]' from code above with simply a '\w'
## '\w' referes to word character, as well as numbers and the underscore
## note that there are others that can be found in the re library
# import re
# email = input("What's your email? ").strip()
# username, domain = email.split("@")
# if re.search(r"^\w+@\w+\.edu$", email):
# print("Valid")
# else:
# print("Invalid")
# ## Furthermore, you can include multiple email domains
# import re
# email = input("What's your email? ").strip()
# username, domain = email.split("@")
# if re.search(r"^\w+@\w+\.(edu|com|gov|net|org)$", email):
# print("Valid")
# else:
# print("Invalid")
# ## However, the above does not address uppercase EDU
# ## Using 're' 'flags', such as 're.IGNORECASE'
# import re
# email = input("What's your email? ").strip()
# username, domain = email.split("@")
# if re.search(r"^\w+@\w+\.(edu|com|gov|net|org)$", email, re.IGNORECASE):
# print("Valid")
# else:
# print("Invalid")
## How to address emails that have two '.' dots after '@'
## i.e. malan@cs50.harvard.edu, this email will return as 'Invalid'
## Here we will be using the '?' as this will make the pattern optional from the code in '()' from the left of the ? mark
# import re
# email = input("What's your email? ").strip()
# username, domain = email.split("@")
# if re.search(r"^\w+@(\w=\.)?\w+\.(edu|com|gov|net|org)$", email, re.IGNORECASE):
# print("Valid")
# else:
# print("Invalid")
## Additionally, emails are popular not to have '.' before the '@'
## To address this we can simple add that in '()'
import re
email = input("What's your email? ").strip()
username, domain = email.split("@")
if re.search(r"^(\w|\.)+@(\w=\.)?\w+\.(edu|com|gov|net|org)$", email, re.IGNORECASE):
print("Valid")
else:
print("Invalid")