EMAIl VALIDATION in java script

Email Validation in Javascript

Email ID is a very important part of a user account. When an application invites the user to create accounts or allow users to subscribe to a newsletter, they ask for an email ID. Email ID is the initial medium of formal communication, so this is mandatory that the email is received should be in a valid format.

Here we are going to explain how to do Email Validation in JavaScript?

An Email is a string with a very simple structure. There are two parts :

  1. unique_id
  2. Domain Name

These two parts are separated by @. 

Suppose interviewsortout@gmail.com is an email where the first part interviewsortout is Unique User ID at the gamil.com domain.

The length of the unique_id part may be up to 64 characters long and domain name may be up to 253 characters.

The first part of an email may have the following characters :

  • May contain Alphabets either in upper case or in lower case.
  • Any Digits 
  • Special character except @  
  • a dot(.) can not be the first and last character of uique_id

The second part is the domain name. So we can have .com, .in, .info, .net, .au etc type of domain. 

Have a look at some valid email addresses:

  • info@interviewsortout.com
  • interviewsortout@gmail.com
  • sibbu2005@gmail.com
  • interview.sortout@gmail.com
  • userid@example.net
  • 23_user_23@example.in

 

Now Have a look on Invalid Email Address : 

  • @not present : [Example.user.com]
  • Top Level Domain can not be started with dot ( . ) : [user@.com.my ]
  • @user.example.net [ No character before @ ]
  •  invalid top level domain   [exampleuser123@gmail.b]
  • an email can not start with “.” [ .user@example.net ]
  • double dots are not allowed [interview..sortout@gmail.com]

 

To do email validation in javascript we will use Regular Expression to match that the given email address matches the criteria of a valid domain.

 

Regular Expression for email validation in JavaScript:

 

/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

 

A complete Solution of Email Validation in Javascript :

Spread the love
  • 1
    Share
Dhananjay

Dhananjay

I’m Dhananjay — a free-spirit with a passion for technologies and blogging.

View all posts by Dhananjay →

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.