Basic algorithms 101 : Checking for email address validity (without regular expressions)

Hello!

Welcome back to “Basic algorithms 101”. Last time we discussed about a coin toss algorithm. It was a fairly easy one. This time we are taking up a slightly more complicated one. It is, checking for the validity of an email address. Basically it’s an algorithm to check if the given email address is a valid one or not.

If you are a pro coder, I suggest you don’t read this because we are not going to using regular expressions but taking a simpler but slightly longer route. If you are a beginner and not comfortable with regular expressions, do not worry, there are other ways too. If you are among the people who have no idea what regular expressions are, I strongly recommend you look it up.


Checking for email address validity is used everywhere. Every time we sign up for a website with our email address, the back-end of the website will check for the validity of the email address we have entered. So this is one of the algorithms which are actually being used in the real world.
Ok, so without any further delay, let’s just get into the algorithm.

LOGIC:

  1. Check whether the given email address has exactly one ‘@’ symbol
  2. Make sure that the user name (all the characters before the ‘@’ symbol) exists and has only alphanumeric characters or legal symbols i.e. dot (.) or underscore (_).
  3. Make sure the domain name exists and has only alphanumeric characters. The domain name is the text between the ‘@’ symbol and the dot (.) on the right side.
  4. There should be at least one dot (.) after the ‘@’ symbol. And not more than 3 alphabets after each dot.  
That is it. That is all it takes to make a simple email address checker.  You can add more checks if you want. But to make it simple, these are enough.

Here is sample code written in python:


def checkValid(s):
    t = "abcdefghijklmnopqrstuvwxyz1234567890._"
    a = len(s)
    if s.count('@') != 1:  # CHECKING FOR '@' SYMBOL
        return False
    for i in range(len(s)):
        if s[i] == '@':
            a = i
        if i > a and s[i] == ".":
            b = i
            break
    user_name = s[:a]
    domain_name = s[a+1:b]
    if len(user_name) < 1 or len(domain_name) < 1 :
        return False
    for e in user_name: # CHECKING FOR PROPER USER NAME
        if e not in t:
            return False
    for e in domain_name:  # CHECKING FOR PROPER DOMAIN NAME
        if e not in t[:-2]:
            return False
    if s[a+1:].count('.') < 1: # COUNTING THE NUMBER OF DOTS AFTER '@'
        return False
    if len(s[b:]) < 2: # COUNTING LETTERS AFTER '.'
        return False
    return True  

 That is the code. Do not be intrigued by the size of the code. I could have made it much smaller but to make it illustrative I have elaborated it more.

I hope you found it helpful. If you have any queries related to today’s topic, please ask in the comments section and I will definitely help you. If you have any suggestions for the “basic algorithms 101” series, please comment below or contact me directly. I will consider all the suggestions.

So that’s it for today. Hope you got to learn a thing or two. Again if you have any questions, suggestion, complains or random thoughts about the blog, please get it touch. Don’t forget to subscribe and share.