jquery script checking if email seems incorrect e.g. hotmail.con

Joined
12 Feb 2006
Posts
17,213
Location
Surrey
on my php form i have the email checked if it's a valid format, however this doesn't check whether it appears to be a correct address, such as if someone types @ntlwirld.com. This will get through as correct, but clearly there's a typo.

I've seen it before on sites that pick this up and say "did you mean @ntlworld.com", but can't find anything online that'll do this.

i'm hoping someone on here might know of something. Ideally I want it in jquery

thanks
 
Soldato
Joined
18 Oct 2002
Posts
15,191
Location
The land of milk & beans
jQuery is a tool for DOM manipulation which isn't entirely what you need for this. To do the comparison of the email domains you could use a POJS lookup object which has a key of the correct domain, and an array containing all the common typos. Then if you find a match on what's been typed in you can show the warning, something like this:

Code:
var lookups = {
  'ntlworld.com': ['ntlwirld.com', 'nttwirld.com'],
  'gmail.com': ['gmaal.com', 'gmale.com']
}

function getKeyByValue(o, v) {
  return Object.keys(o).find(k => o[k].indexOf(v) != -1);
}

$(function() {
  $('#email').on('input', function() {
    var domain = (this.value || '').trim().split('@').pop();
    var alternateDomain = (getKeyByValue(lookups, domain) || '');
    $('#warning').text(`Did you mean @${alternateDomain}?`).toggle(alternateDomain !== '');
  });
});


Working example: https://jsfiddle.net/c68q5sub/2/

I'll leave writing the list of typos for every email provider you can think of as an exercise for yourself :)
 
Back
Top Bottom