195 lines
7.5 KiB
JavaScript
195 lines
7.5 KiB
JavaScript
|
|
|
|
$(document).ready(function(){
|
|
$(document).on('focus', 'input[name="email"]', function(){
|
|
emailValidator.emailInit($(this));
|
|
});
|
|
});
|
|
|
|
|
|
/** ==============
|
|
* Email validation
|
|
=============== */
|
|
|
|
var i18n = {
|
|
form_email_suggestion: 'Vouliez-vous dire {0} ?'
|
|
};
|
|
|
|
var emailValidator = {
|
|
domains : ['gmail.com', 'hotmail.fr', 'hotmail.com', 'yahoo.fr', 'orange.fr', 'free.fr', 'wanadoo.fr', 'live.fr', 'laposte.net', 'yahoo.com', 'sfr.fr', 'msn.com', 'neuf.fr', 'aol.com', 'voila.fr', 'facebook.com', 'me.com', 'orange.com', 'club-internet.fr', 'bbox.fr', 'noos.fr', 'outlook.fr', 'microsoft.com', 'bnpparibas.com', 'numericable.fr', 'aliceadsl.fr', 'outlook.com', 'ymail.com', 'googlemail.com', 'gmx.de', 'icloud.com', 'capgemini.com', 'web.de', 'cegetel.net', 'edhec.com', 'loreal.com', 'mac.com', 'essec.edu', 'gmail.fr', 'deloitte.fr', 'sfr.com', 'hec.edu', 'renault.com', 'google.com', 'gmx.fr', 'gadz.org', 'sncf.fr', 'leetchi.com', 'dbmail.com', 'live.com', 'libertysurf.fr', 'sgcib.com', 'skynet.be', 'aol.net', '9online.fr', 'bouyguestelecom.fr', 'gmx.net', 'mail.com'],
|
|
secondLevelDomains : ['gmail', 'hotmail', 'hotmail', 'yahoo', 'orange', 'free', 'wanadoo', 'live', 'laposte', 'yahoo', 'sfr', 'msn', 'neuf', 'aol', 'voila', 'facebook', 'orange', 'club-internet', 'bbox', 'noos', 'outlook', 'microsoft', 'bnpparibas', 'numericable', 'aliceadsl', 'outlook', 'ymail', 'googlemail', 'gmx', 'icloud', 'capgemini', 'cegetel', 'edhec', 'loreal', 'mac', 'essec', 'gmail', 'deloitte', 'sfr', 'hec', 'renault', 'google', 'gmx', 'gadz', 'sncf', 'leetchi', 'dbmail', 'libertysurf', 'skynet', 'aol', '9online', 'bouyguestelecom'],
|
|
topLevelDomains : ['fr', 'be', 'de', 'es', 'it', 'eu', 'co.uk', 'uk', 'nl', 'at', 'ca', 'com', 'info', 'net', 'org', 'bzh', 'name', 'cat', 'fi', 'se', 'gf', 'gy', 'pl', 'ie', 'il', 'lu', 'ma', 're', 'ro'],
|
|
emailInit : function(element) {
|
|
if (element.parent().children('.form-email-helper').length)
|
|
return;
|
|
element.after($('<span></span>').addClass('form-email-helper hidden'));
|
|
// Add empty label to align input and suggestion
|
|
if (element.prev().prop('tagName') === 'LABEL')
|
|
element.after($('<label> </label>').addClass('label-email-helper hidden'));
|
|
|
|
return element.on('blur', function() {
|
|
return element.mailcheck({
|
|
domains: emailValidator.domains,
|
|
secondLevelDomains: emailValidator.secondLevelDomains,
|
|
topLevelDomains: emailValidator.topLevelDomains,
|
|
suggested: function(element, suggestion) {
|
|
return emailValidator.emailSuggest(element, suggestion);
|
|
},
|
|
empty: function(element) {
|
|
return emailValidator.emailEmpty(element);
|
|
}
|
|
});
|
|
});
|
|
},
|
|
emailSuggest : function(element, suggestion) {
|
|
var helper, helper_label;
|
|
helper = element.closest('.form-group').find('.form-email-helper');
|
|
helper.removeClass('hidden');
|
|
helper_label = element.closest('.form-group').find('.label-email-helper');
|
|
if (!helper.hasClass('active')) {
|
|
helper_label.removeClass('hidden');
|
|
helper.addClass('active');
|
|
}
|
|
helper.html(i18n.form_email_suggestion.replace('{0}', '<button class="btn-link form-email-helper-link" type="button">' + suggestion.full + '</button>'));
|
|
return helper.find('.form-email-helper-link').one('click', function() {
|
|
element.val($(this).text());
|
|
element.closest('.form-group').removeClass('form-error').addClass('form-ok');
|
|
helper_label.addClass('hidden');
|
|
helper.addClass('hidden');
|
|
return helper.removeClass('active').html('');
|
|
});
|
|
},
|
|
emailEmpty : function(element) {
|
|
var helper, helper_label;
|
|
helper = element.closest('.form-email-container').find('.form-email-helper');
|
|
helper_label = element.closest('.form-group').find('.label-email-helper');
|
|
if (helper.hasClass('active')) {
|
|
helper_label.addClass('hidden');
|
|
return helper.removeClass('active').html('');
|
|
}
|
|
}
|
|
};
|
|
|
|
/*! mailcheck v1.1.0 @licence MIT */
|
|
var Mailcheck = {
|
|
domainThreshold: 4,
|
|
topLevelThreshold: 3,
|
|
defaultDomains: ["yahoo.com", "google.com", "hotmail.com", "gmail.com", "me.com", "aol.com", "mac.com", "live.com", "comcast.net", "googlemail.com", "msn.com", "hotmail.co.uk", "yahoo.co.uk", "facebook.com", "verizon.net", "sbcglobal.net", "att.net", "gmx.com", "mail.com", "outlook.com", "icloud.com"],
|
|
defaultTopLevelDomains: ["co.jp", "co.uk", "com", "net", "org", "info", "edu", "gov", "mil", "ca"],
|
|
run: function(a) {
|
|
a.domains = a.domains || Mailcheck.defaultDomains, a.topLevelDomains = a.topLevelDomains || Mailcheck.defaultTopLevelDomains, a.distanceFunction = a.distanceFunction || Mailcheck.sift3Distance;
|
|
var b = function(a) {
|
|
return a
|
|
},
|
|
c = a.suggested || b,
|
|
d = a.empty || b,
|
|
e = Mailcheck.suggest(Mailcheck.encodeEmail(a.email), a.domains, a.topLevelDomains, a.distanceFunction);
|
|
return e ? c(e) : d()
|
|
},
|
|
suggest: function(a, b, c, d) {
|
|
a = a.toLowerCase();
|
|
var e = this.splitEmail(a),
|
|
f = this.findClosestDomain(e.domain, b, d, this.domainThreshold);
|
|
if (f) {
|
|
if (f != e.domain) return {
|
|
address: e.address,
|
|
domain: f,
|
|
full: e.address + "@" + f
|
|
}
|
|
} else {
|
|
var g = this.findClosestDomain(e.topLevelDomain, c, d, this.topLevelThreshold);
|
|
if (e.domain && g && g != e.topLevelDomain) {
|
|
var h = e.domain;
|
|
return f = h.substring(0, h.lastIndexOf(e.topLevelDomain)) + g, {
|
|
address: e.address,
|
|
domain: f,
|
|
full: e.address + "@" + f
|
|
}
|
|
}
|
|
}
|
|
return !1
|
|
},
|
|
findClosestDomain: function(a, b, c, d) {
|
|
d = d || this.topLevelThreshold;
|
|
var e, f = 99,
|
|
g = null;
|
|
if (!a || !b) return !1;
|
|
c || (c = this.sift3Distance);
|
|
for (var h = 0; h < b.length; h++) {
|
|
if (a === b[h]) return a;
|
|
e = c(a, b[h]), f > e && (f = e, g = b[h])
|
|
}
|
|
return d >= f && null !== g ? g : !1
|
|
},
|
|
sift3Distance: function(a, b) {
|
|
if (null == a || 0 === a.length) return null == b || 0 === b.length ? 0 : b.length;
|
|
if (null == b || 0 === b.length) return a.length;
|
|
for (var c = 0, d = 0, e = 0, f = 0, g = 5; c + d < a.length && c + e < b.length;) {
|
|
if (a.charAt(c + d) == b.charAt(c + e)) f++;
|
|
else {
|
|
d = 0, e = 0;
|
|
for (var h = 0; g > h; h++) {
|
|
if (c + h < a.length && a.charAt(c + h) == b.charAt(c)) {
|
|
d = h;
|
|
break
|
|
}
|
|
if (c + h < b.length && a.charAt(c) == b.charAt(c + h)) {
|
|
e = h;
|
|
break
|
|
}
|
|
}
|
|
}
|
|
c++
|
|
}
|
|
return (a.length + b.length) / 2 - f
|
|
},
|
|
splitEmail: function(a) {
|
|
var b = a.trim().split("@");
|
|
if (b.length < 2) return !1;
|
|
for (var c = 0; c < b.length; c++)
|
|
if ("" === b[c]) return !1;
|
|
var d = b.pop(),
|
|
e = d.split("."),
|
|
f = "";
|
|
if (0 == e.length) return !1;
|
|
if (1 == e.length) f = e[0];
|
|
else {
|
|
for (var c = 1; c < e.length; c++) f += e[c] + ".";
|
|
e.length >= 2 && (f = f.substring(0, f.length - 1))
|
|
}
|
|
return {
|
|
topLevelDomain: f,
|
|
domain: d,
|
|
address: b.join("@")
|
|
}
|
|
},
|
|
encodeEmail: function(a) {
|
|
var b = encodeURI(a);
|
|
return b = b.replace("%20", " ").replace("%25", "%").replace("%5E", "^").replace("%60", "`").replace("%7B", "{").replace("%7C", "|").replace("%7D", "}")
|
|
}
|
|
};
|
|
"undefined" != typeof module && module.exports && (module.exports = Mailcheck), "function" == typeof define && define.amd && define("mailcheck", [], function() {
|
|
return Mailcheck
|
|
}), "undefined" != typeof window && window.jQuery && ! function(a) {
|
|
a.fn.mailcheck = function(a) {
|
|
var b = this;
|
|
if (a.suggested) {
|
|
var c = a.suggested;
|
|
a.suggested = function(a) {
|
|
c(b, a)
|
|
}
|
|
}
|
|
if (a.empty) {
|
|
var d = a.empty;
|
|
a.empty = function() {
|
|
d.call(null, b)
|
|
}
|
|
}
|
|
a.email = this.val(), Mailcheck.run(a)
|
|
}
|
|
}(jQuery);
|
|
|
|
|
|
/** ==================
|
|
* END Email validation
|
|
==================== */ |