I am trying to reduce bounce rates of emails sent from the website I am building. After looking at the list of registered users I have noticed that quite often people misspell d
I have found Mailcheck.js on Github which does exactly what I want by providing suggestion like "Did you mean user@gmail.com?"
However, library is for plain Javascript / jQuery. I needed AngularJs wrapper so I slightly modified angular-mailcheck here is resulting directive:
(function () {
'use strict';
/**
* @ngdoc directive
* @name mailcheck.directive:mailcheck
* @description
* Angular wrapper for Mailcheck.js
*/
function mailcheckDirective($compile, $sce) {
return {
restrict: 'A',
replace: false,
link: function (scope, el, attrs) {
//Mailcheck.defaultDomains.push('yandex.ru', 'rambler.ru', 'bk.ru', 'ukr.net', 'list.ru', 'inbox.ru', 'yandex.ua', 'ya.ru', 'i.ua', 'inbox.lv', 'mail.ua', 'yandex.com', 'abv.bg', 'icloud.com', 'meta.ua', 'tut.by', 'rediffmail.com');
Mailcheck.defaultTopLevelDomains.push('com.id', 'com.ph', 'com.br', 'com.vn', 'com.in');
// Limit to input element of specific types
var inputTypes = /text|email/i;
if (el[0].nodeName !== 'INPUT') {
throw new Error('angular-mailcheck is limited to input elements');
}
if (!inputTypes.test(attrs.type)) {
throw new Error('Invalid input type for angular-mailcheck: ' + attrs.type);
}
scope.suggestion = false;
scope.bugmenot = false;
// Compiled template
if (attrs.mailcheck !== "notemplate") {
var template = $compile('<div class="help-block mailcheck" ng-show="suggestion && !bugmenot">Did you mean <a ng-bind="suggestion" ng-click="useSuggestion()"></a>? <a ng-click="suggestion=false;bugmenot=true">Nope.</a></div>')(scope);
el.after(template);
}
el.bind('input', function () {
scope.suggestion = false;
})
.bind('blur', function () {
el.mailcheck({
suggested: function (element, suggestion) {
scope.suggestion = suggestion.full;
scope.$apply();
},
empty: function (element) {
scope.suggestion = false;
}
});
});
scope.useSuggestion = function () {
el.val(scope.suggestion);
scope.suggestion = false;
};
}
};
}
angular
.module('angular-mailcheck', [])
.directive('mailcheck', mailcheckDirective);
mailcheckDirective.$inject = ['$compile', '$sce'];
})();
Once directive is part of solution it can be used like this in HTML:
<input mailcheck="notemplate" />
<small class="mailcheck" ng-show="suggestion && !bugmenot">
<span>Did you mean</span> <a ng-bind="suggestion" ng-click="useSuggestion()"></a>?
<a ng-click="suggestion=false;bugmenot=true">Nope</a>.
</small>
If you don't need to customize mailcheck block in HTML you can user mailcheck=""
attribute instead of mailcheck="notemplate"
.