AngularJs directive to offer suggestions for misspelled emails?

后端 未结 1 1260
你的背包
你的背包 2021-01-26 13:07

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

相关标签:
1条回答
  • 2021-01-26 14:13

    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".

    0 讨论(0)
提交回复
热议问题