Validating Email using Python

Python is a quite simple and powerful programming language in the sense that it can be applied to so many areas like Scientific Computing, Natural Language Processing but one specific area of application of Python which I found quite fascinating is => Using it for Syntax Validations.

The Software systems which accept User-Input need to first check whether User entered what is requested or not. If there is a box which says to enter Email Address and if User entered Mobile Number, and you’ve not implemented any Validation Checking and just send that information to your software backend then it may break your software. Most famous of these is DOS(Denial of Service) attack which Hackers use for getting into servers of websites, using those pages of website which ask for User Input. (Unfortunately this have happened to some of my friend’s websites)
Anyway in order to make your Software System more reliable, secure and robust => Any kind of User Input should be validated before sending that input to Backend of Software System.

In this article, I’ll discuss about How to validate Email using Python? also same logic which I’ll be explaining in this article can be used for Validating Login System for a Python Django Website.

Let’s discuss how to validate an Email Address using Python Programming Language.

Using Python re Module for Validating Email

Below are steps for validating email using python =>

  1. First import Python’s re module into code file using import re statement
  2. Make a Regex which matches against usual email syntaxes like example@gmail.com, exam@harvard.edu, developer@python.org
  3. Regex for matching these Email Syntaxes is ^[a-z0-9]+[._]?[a-z0-9]+[@]\w+[.]\w{2,3}$
  4. Use re module’s search(regex, email) function for matching email addresses against Regex
  5. If search(regex, email) function returns True then email is valid, otherwise not

Let’s put all of these 5 steps together as Python Code for validating an email address =>

# Import Python's re module
import re 

# Regex for checking Email Syntax
regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'

# Valid Email checking Function
def check(email):  
	if(re.search(regex,email)): 
		print(email, " => is valid") 
	else: 
		print(emai, " => is not valid") 

check("example@gmail.com")
check("example@harvard.edu")
check("example.Josh@python.org")

Output of above code will be =>

example@gmail.com  => is valid
example@harvard.edu  => is valid
example.Josh@python.org  => is not valid

This is the simplest approach for validating whether an email address is correct or not using Python Programming Language.

Also if in case, your not that good in understanding Regular Expressions then let me explain a little about How Regex used in above Python Code for validating Email actually works.

^[a-z0-9]+[._]?[a-z0-9]+[@]\w+[.]\w{2,3}$

  • ^[a-z0-9]+ => Means start with either number or alphabet and have any of these any number of times. Like 1736gdbeyujevb, vydevuce8929878 or something like this
  • [\._]? => Means either have or not have dot/underscore. Like josh. or josh_
  • [a-z0-9]+ => Means have either number or alphabet any number of times. Like 10403hjdbece, beuceb20900
  • [@] => Must be a At Sign. Like josh@ or computersciencehub@
  • \w+ => Means have any number of just alphabets like gmail, yahoo
  • [.] => Means have a dot
  • \w{2, 3} => Means have two or three alphabetic characters like com or org

I hope that this short kind of tutorial helped you to understand How is Email Regex Expression Designed and how to validate an email address using Python Programming Language.

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts