简体   繁体   English

JQuery验证表单,但不是“提交”按钮

[英]JQuery validation form but not `submit` button

I'm using the JQuery.Validation plugin to validate some fields in a form, but I don't want to use a submit button, I want to use just an <input type="button"> and once clicked using JQuery I want to call my controller, the only thing is that it's not a submit button, so even if the fields are wrong or not validated it will call the controller even if the fields values are wrong, because I'm using the .click() event of JQuery when that button is clicked, I want when I have wrong values on the fields and the validation is showing an error message not to call my action controller even if the button is clicked, how can I reach this? 我正在使用JQuery.Validation插件验证表单中的某些字段,但我不想使用submit按钮,我只想使用<input type="button">并且一次点击使用我想要的JQuery调用我的控制器,唯一的问题是它不是一个submit按钮,所以即使字段错误或未经过验证,即使字段值错误也会调用控制器,因为我正在使用.click()事件单击该按钮时的JQuery ,我希望当我在字段上有错误的值并且验证显示错误消息,即使单击该按钮也不会调用我的动作控制器,我该如何达到此目的? This is my html code: 这是我的html代码:

<script src="~/js/jquery.validate.js"></script>
<form action="post" class="rightform newsletter" id="formSubscription">
    <div class="title">
        <img src="http://ligresources.blob.core.windows.net/public/UK/Content/Images/newsletter.png" alt="">NEWSLETTER
    </div>
    <input type="text" id="txtFullName" name="fullName" placeholder="Full name" required>
    <input type="email" id="txtEmail" name="email" placeholder="Email address" required>
    <button type="button" id="btnSubscription" data-url="@Url.Action("SubscriptionEmail","Email")" style="background-color:#f66804;">SUBSCRIBE TO NEWSLETTER</button>                
</form>

This is the JQuery.Validation plugin code: 这是JQuery.Validation plugin代码:

$(document).ready(function () {
    $("#formSubscription").validate({
        rules: {
            email: {
                required:true,
                email:true
            },
            fullName: {
                required:true
            }
        },
        messages: {
            email: {
                required: "Please enter an email address",
                email: "Please enter a valid email address"
            }
        }
    });
});

And this is the JQuery function that I have for the moment to throw when the btnSubscription is clicked : 这是我clicked btnSubscription时暂时抛出的JQuery函数:

$(document).ready(function () {
    $("#btnSubscription").click(function () {
        SendSubscription();          
    }),
    return false;
}),

function SendSubscription() {       
    $.ajax({
        type: "post",
        url: $("#btnSubscription").data("url"),
        data: { fullName: $("#txtFullName").val(), emailAddress: $("#txtEmail").val() },
        success: function () {
            alert("email sent")
        },
        error: function () {
            alert("An error occurred..")
        }
    });
}

Once you set up your .validate() all you need to do is call the .valid() method in your .click() handler. 一旦您设置.validate()所有你需要做的是调用.valid()在你的方法.click()处理程序。 This is what submit does automatically, but you can call it manually with the .valid() method. 这是提交自动执行的操作,但您可以使用.valid()方法手动调用它。

you should use type submit , and then you can use the submitHandler in order to manage other things you want to do, like this: 你应该使用类型submit ,然后你可以使用submitHandler来管理你想要做的其他事情,如下所示:

$("#formSubscription").validate({
  submitHandler: function(form) {
    // do other things for a valid form
    form.submit();
  }
});

Take a loot at the documentation: https://jqueryvalidation.org/validate/ 在文档中获取战利品: https//jqueryvalidation.org/validate/

put an if in the SendSubscription function: 在SendSubscription函数中添加一个if:

if (yourValidationDidntPass) {
  //do whatever you want
}
else {
  $.ajax({...});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM