Building a self-correcting email input field

Email is one of the common input fields in any web form. Many sites use two email fields to make sure that the user has entered correct email ids. This is a common idiom in web design, and it is surprising that it has not changed over all these years. One of the interesting alternative I found is the ‘MailCheck’ JavaScript library. The library enables you to offer the user a suggestion for the domain name if he makes a mistake while entering. A demo is shown below.

Enter an email id in the ‘E-mail’ field below with an incorrect domain name, like ‘’ and change the focus to the ‘Name’ field. The form should then offer a suggestion on the correct domain name. Note that the script will only try to check for incorrect name in the domain part of the email address and not the local part, the one before the ‘@’ sign. The script also checks for the correct top level domain (TLD).

The script works by finding a similar domain name in its list of names and returns a domain name that is within a certain distance between the incorrect domain name entered. This post will explain how to integrate the MailCheck library into your web form.

Installation & Setup

Installation is simple. Download mailcheck.min.js from GitHub into your JavaScript directory along with a local or CDN copy of jQuery.

Define your email input field along with your other fields. For example, the fields for the above demo are given below. The suggest element will later hold the suggested domain returned by Mailcheck.

Add the ‘MailSend’ initialization script to your page, preferably at the bottom. The following code is then called whenever the ’email’ input field loses focus.

Mailcheck takes in two callbacks, suggested and empty. The suggested callback is fired when there’s a suggestion for a possible incorrect email domain. empty callback is fired when there is no suggestion. Mailcheck passes in the target element and the suggestion. The suggestion object has the following members, in the above code we have used the full attribute:

  address: 'test',          // the address; part before the @ sign
  domain: '',    // the suggested domain
  topLevelDomain: 'com',    // the suggested top level domain
  full: ''  // the full suggested email

Mailcheck compares the entered email ids domain with the ones included in the library, which includes the following. You can add additional domain by editing the ‘mailcheck.js’ file.

"", "", "", "", "", 
"", "", "", "", "", 
"", "", "", "",
"","", "", "", ""

It also include the following top level domains.

"", "com", "net", "org", "info", "edu", "gov", "mil"

How does the domain suggestion work

Mailcheck relies on a String Edit Distance algorithm to find strings that are within a certain distance of each other. Also know as ‘Levenshtein distance‘; Informally, the Levenshtein distance between two words is equal to the number of single-character edits required to change one word into the other.

For example ‘’ and ‘’ are 1 distance apart. ‘’ and ‘’ are 1.5 distance apart. The MailCheck library edit-distance threshold is set to 3. i.e, string which have a edit distance greater than 3 are ignored.

Mailcheck uses the ‘Sift3‘ algorithm to compute the Levenshtein distance between two strings, but you can also use your own algorithm if you think that it is superior or more to your requirement.

Changing default options

You can override the default options, like the domain list, top level domain names, and the algorithm used. For example, the following code restricts the domains to hotmail, gmail and yahoo and uses a custom distance algorithm (not given here). You can also override the default ‘Sift3’ algorithm with your own. But for most general purposes the defaults should work fine.

4 thoughts to “Building a self-correcting email input field”

  1. Very nice. I’ve always found the “Verify e-mail”-fields to be reduntant as I’m sure most people like me just do a copy/paste from their first e-mail entry field.

    But I think while at it, why not use php’s getmxrr?

    DNS lookups are fast and inexpensive in terms of processing, and is much less likely to return false positives.

    See discussion at for more methods on using getmxrr for email validation.

  2. Ah, sorry – came here via some php-related website, and didn’t see until I had posted that this article didn’t mention php at all – only the TP.

    I guess my habits of using ajax calls and server side processing have become too much a routine 🙂

Leave a Reply

Your email address will not be published.