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.
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
if(isValid("[email protected]") == True):
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.