Skip to content

nadinengland/email-typo-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

email-typo-js

Inspired by a tweet from @jakemarsh, email-typo-js will give you a list of alternatives if it believes that the email's Top Level Domain isn't valid. The most common use case for this is the classic @whatever.con, clearly this should be @whatever.com (take note of the 'm' instead of 'n'). A very simple live demo is available for your eyes.

Usage

Using email-typo-js is as simple as loading release/email-typo.js into your JS environment, whether that be as a module or the browsers script tag.

Simple

// returns an %array% of alternatives, or %null%
var alternatives = EmailTypo.alternatives("[email protected]"); // => ['.com']

DOM Example

<input id='email' type='email' value='[email protected]' />
document.getElementById('email').onkeypress = function () {
  var email = this.value
    , alternatives = EmailTypo.alternatives(this.value());

  if (alternatives !== null && alternatives.length > 0) {
    alert(this.value + " => did you mean " + alternatives.toSentence("or") + "?");
    // email@email.con => did you mean .com?
  }
};

For String.prototype.toSentence check out Sugar, I recommend it regardlessly.

Email Validation

It should be noted that email-typo-js only checks the Top Level Domain, if the string doesn't end with .x things will go wrong. It is recommended that you validate the email prior to checking for TLD typos.

EmailTypo.alternatives("email");        // => null
EmailTypo.alternatives("email.con");    // => ['.com']
EmailTypo.alternatives("@email.co.uj"); // => ['.uk']

// Validate first
email = "[email protected]";
if (email.match(regExp) !== null && EmailTypo.alternatives(email) === null) {
  // We possibly have a valid email
}

Extending Known Typo List

It is more than likely that you may need to cater for a typo that email-typo-js doesn't know about. Pass a object with typo's as keys to extend the known list for that call. Each key should have an array of alternatives.

EmailTypo.alternatives("[email protected]", {
  'examplr' : [
    'example',
    'exemple',
    'esempio'
  ]
});   // => ['.example', '.exemple', '.esempio']

Contributions

Please fork and submit pull requests, or submit issues via github!

To Do

  • Make NodeJS package
  • JSMin the code
  • Actually, create a script to do the min and create a release
  • create a release directory and add a release!

About

A simple JS library for handling common email typos

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published