mailto提交验证错误

时间:2018-01-08 16:45:26

标签: jquery validation

我正在使用jquery验证。 我相当肯定这里有序列问题。

这是发生了什么事。

我有一个mailto链接,它不是提交按钮,而是一个锚标记,因为我需要mailto属性。

使用JS填充mailto:。

问题是,当我单击按钮提交时,我的$("表单")。submit()正在发生,验证也在发生,但随后它继续打开mailto:属性。如果有没有填充的字段,我不想要。

我理解这是因为在click事件中,mailto没有任何阻止它继续下一行代码。如果验证有错误,我怎么能说提交,不要运行下一段代码。但如果它没有错误,请运行下一段代码。

有些代码被遗漏了,因为它并不重要,但这是我的代码。

</section>
<!-- end feedback -->
<div class="email">
    Enter your email:
    <input type="email" size="50" placeholder="example@example.com" name="email" class="email-value" required>
</div>

<a href="" class="btn btn-danger btn-lg m-0a mb-5 mt-3 cursor-pointer email-link">Email Results</a> 

和js ......

$("form").validate({
        "rules": {
            "question-1": "required",
            "question-2": "required",
            "question-3a": "required",
            "question-3b": "required",
            "question-3c": "required",
            "question-4": "required",
            "question-5": "required",
            "question-6": "required",
            "question-7": "required",
            "email": {
                "required": true
            }
        },
        "messages": {
            "question-1": "Required",
            "question-2": "Required",
            "question-3a": "Required",
            "question-3b": "Required",
            "question-3c": "Required",
            "question-4": "Required",
            "question-5": "Required",
            "question-6": "Required",
            "question-7": "Required",
            "email": "Email is required to send the results."
        }
    });

    $(".email-link").on("click", function(e) {  
            $("form").submit()
            //THIS IS WHERE I THINK I NEED TO STOP THE QUERY FROM EXECUTING UNTIL VALIDATION IS FINE.
            var emailAddress = $("input[name=email]").val();
            var subject = "How%20Asthma%20Friendly%20Is%20Your%20School?";
            var response = "Thanks%20for%20completing%20the%20American%20Lung%20Association%20Assessment%2C%20How%20Asthma%20Friendly%20Is%20Your%20School%3F%20%0AYour%20answers%3A" +
            "%20%0AQuestion%201%3A%20" + $("input[name=question-1]:checked").val() + 
            "%20%0AQuestion%202%3A%20" + $("input[name=question-2]:checked").val() + 
            "%20%0AQuestion%203a%3A%20" + $("input[name=question-3a]:checked").val() + 
            "%20%0AQuestion%203b%3A%20" + $("input[name=question-3b]:checked").val() + 
            "%20%0AQuestion%203c%3A%20" + $("input[name=question-3c]:checked").val() + 
            "%20%0AQuestion%202%3A%20" + $("input[name=question-4]:checked").val() +
            "%20%0AQuestion%202%3A%20" + $("input[name=question-5]:checked").val() +
            "%20%0AQuestion%202%3A%20" + $("input[name=question-6]:checked").val() +
            "%20%0AQuestion%202%3A%20" + $("input[name=question-7]:checked").val();

            $(this).attr({
                href: "mailto:" + emailAddress + "?subject=" + subject + "&body=" + response
            });
    });

任何想法?

1 个答案:

答案 0 :(得分:0)

如果有人遇到同样的问题。这是解决方案。

User.findOneAndUpdate({$or: [
              {'local.username': username }, // 'or' condition works perfect
           { 'facebook.username': username },
            { 'twitter.username': username },
  ]},{
    email:req.body.email // doesn't work...here i don't know what to do... 
  },function(err, user) {
            if(err){console.log(err);} 
              if(!user){
                console.log('cannot find user');
              }
              if(user){              
                console.log(user);
                   return res.send(JSON.stringify(user));

            }
        });