AngularJs指令为拼写错误的电子邮件提供建议?

时间:2015-12-24 03:39:07

标签: javascript c# jquery asp.net angularjs

我正在尝试降低从我建立的网站发送的电子邮件的跳出率。在查看注册用户列表后,我注意到人们常常在电子邮件中拼错域名,例如 user@gmai.com user@gmail.con

除了以某种方式绑定MX查询和AngularJs之外,我有什么选择在浪费反弹之前验证该电子邮件地址是否正确?

1 个答案:

答案 0 :(得分:0)

我发现Mailcheck.js on Github通过提供类似&#34的建议而完全符合我的要求;您的意思是 user@gmail.com ?"

但是,库是用于普通的Javascript / jQuery。我需要AngularJs包装器,所以我稍微修改了angular-mailcheck这里是结果指令:

(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'];

})();

一旦指令成为解决方案的一部分,就可以在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>

如果您不需要在HTML中自定义mailcheck块,则可以使用mailcheck=""属性而不是mailcheck="notemplate"