I recently stumbled across mailcheck.js, a little jQuery plugin from Kicksend that suggests domains based on common typos in email forms. For example, 'user@gnail.co' will generate a suggestion for 'user@gmail.com'. It's perfect for preventing errors in user signups, and the authors claim its reduced their email bounces by 50%. After playing around with it, I've decided to bundle it into production for most, if not all of my projects, and this is just a brief demo of what can be done with it.
Getting Set Up
Our goal is to create a simple display that shows email suggestions to the user and offers a way to automatically fill in the field with the suggestion. The plugin is used in two steps - first, it has to be attached to a text field, and then you have to actually do something with its suggestions. Let's get the basic JavaScript structure set up (make sure you include the actual plugin on your page as well!)
<!-- The HTML -->
<input id="email" type="text" placeholder="Email">
<div id="hint"></div>
// The JavaScript
var $email = $('#email'); // Cache jQuery objects into variables
var $hint = $("#hint");
$email.on('blur', function() {
$(this).mailcheck({
suggested: function(element, suggestion) {
}
});
});
The setup is only a few lines, although it's important to understand what's going on. Our HTML is fairly straightforward - we start with a generic input that we'll be using the plugin on. The <div id="hint">
is an initially hidden div that will contain our suggestions to the user. In our JavaScript, we're first sticking jQuery objects into variables for convenience. Then we're attaching the mailcheck()
function to our email field on its blur event, which is called whenever the element loses focus (i.e., the user moves on to the next field). Mailcheck takes two callbacks, suggested()
and empty()
. suggested()
is called whenever a suggestion is available for the field, and empty()
whenever the field is left blank. We'll only be using suggested()
in this demo, although depending on how you use the plugin it's generally a good idea to use both.