jQuery验证-阻止某些电子邮件域

时间:2018-12-03 00:27:45

标签: jquery validation

我无法扩展验证脚本。我们要阻止来自某些域名的提交,例如aol,hotmail和其他随机电子邮件地址。这就是我到目前为止所拥有的。

我认为我可能只差一点就走了,但我不能完全按时完成。我们尝试了不同的实现,但是我们正在考虑使用验证器插件更有意义。

有什么想法我可能做错了吗?

(function($) {
    jQuery.validator.setDefaults({
        debug: true,
        success: "valid"
    });

    // Setting default message for email validation
    $.validator.messages.email = 'Enter A Valid Email Address';
    //$.validator.messages.email_domain = 'Enter A Valid Email Address';


    var _this = this;

    function validateLargeForm(formId, pageModalId) {
        // Marketo dynamically adds inputs
        $(document).on('keyup', 'input', function(){
            if($(this).valid() == true) {
                var name = $(this).attr('name');
                var label = $(formId + " label[for='" + name + "']")
                label.addClass('success');
            };
        });
        /*-----------------------------------------------------*/
    /*-----------------------------------------------------*/
    $.validator.addMethod("checkDomain", function(value, element) {
        var evilDomains = ["yahoo", "gmail", "aol", "hotmail"];
        var domainEntered = value.replace(/.*@(.*)\\..*/, "$1");
        var isAcceptableDomain = true;

        if($.inArray(domainEntered, evilDomains) >= 0){
          isAcceptableDomain = false;
        }
          return this.optional(element) || (isAcceptableDomain);
      }, "This email will not work.");  
    /*-----------------------------------------------------*/
    /*-----------------------------------------------------*/
        $(formId).validate({
            rules: {
                FirstName: "required",
                LastName: "required",
                Company: "required",
                Primary_Interest__c: "required",
                Company_Size__c: "required",
                Phone: "required",
                Email: {
                  required: true,
                  email: true,
                  checkDomain: true
                },
                MktoCompanyNotes: "required"
            },
            errorPlacement: function(error,element) {
                error.appendTo(formId + " label[for='" + element.attr("name") + "']");
                $(formId + " label[for='" + element.attr("name") + "']").css({
                    'color' : '#DB4C49'
                });
                console.log(error);
            },
            submitHandler: function(formId) {
                if(pageModalId == '#mktoForm_1') {
                    $(pageModalId).hide();
                    $('.mktoForm_1').show();
                } else if(pageModalId == '#mktoForm_2') {
                    $(pageModalId).hide();
                    $('.success-message-subscribe').show();
                } else {
                    $(pageModalId + " form, " + pageModalId + " .form-header").hide();
                    $(pageModalId + " .success-message").show();

                    //check if hash tag exists in the URL
                    if(window.location.hash) {

                        //set the value as a variable, remove "launch-"
                        var hash_value = window.location.hash.replace('launch-','');

                        //show the value with an alert pop-up
                        // console.log(hash_value);

                        var hash_confirmation = hash_value + "-confirmation";
                        // console.log(hash_confirmation);

                        // update hash in url so that analytics team can see a confirmation url
                        history.pushState(null, null, hash_confirmation);                    
                    }

                    //$(pageModalId + " .success-message").append('<div><img height="1" width="1" style="display:none;" alt="" src="https://dc.ads.linkedin.com/collect&fmt=gif" /></div>');
                    //fbq('track', 'GetQuote Submit Lead');

                }

                $(formId.id).submit();
            }
        });
    }

    // trial modal, get in touch modal, lead gen in page form container
    validateLargeForm('#mktoForm_1', '#pop-out');


    // validateSmallForm('#subscribe-form', '#subscribe-form-container');


})(jQuery);

0 个答案:

没有答案