In this post, we will see how to validate email address in Python.
There are times when you want to validate email address. You can write your own code to validate email address but there are many regular expressions which you can use to validate email address.
We can use regular expression provided by OWASP Validation Regex Repository which is considered to be safe and will solve the purpose in most of the cases.
Here is the regular expression:
^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$
Here is complete python program to compare email address.
1 2 3 4 5 6 7 8 9 10 11 |
import re def isValid(email): if(re.match("^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$", email) != None): return True return False print("This is a valid email address") else: print("This is not a valid email address") |
Output:
This is a valid email address
That’s all about how to validate email address in Python.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.