如果验证失败如何停止表单提交 [英] How to stop Form submit if the validation fails

查看:28
本文介绍了如果验证失败如何停止表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我的问题更多是关于如果验证失败如何防止表单提交

UPDATE: my question is more about How to prevent the form submit if the validation fails

链接没有解决我的问题

只需重复我正在做的事情:我有一个带有一堆输入字段的表单,当用户点击提交按钮时,它使用属性 required="required" 验证表单,如果表单验证失败,那么我想停止提交表单,如您所见当用户提交表单时显示 div 的 javascript.

just re-iterate what I'm doing: I have a form with bunch of input fields and when the user hit the submit button it validate form using attribute required="required" if the form validation fails THEN I want to stop submitting the form as you see the javascript it show the div when the user submit the form.

希望这一切都解决了.

结束更新

如果表单验证失败,如何阻止表单提交触发?

How can I stop the form submit to fire if the form validation fails?

<input class="form-control" data-val="true" id="SerialNumber" name="SerialNumber" required="required" type="text">

<button type="submit" name="rsubmit" class="btn btn-success">Submit</button>

 //loading message:
 $("form").submit(function (e) {
     $("#loading").show();
 }); 

推荐答案

我解决了这个问题:

首先在您的页面上添加引用:

First add the ref on your page:

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js"></script>

然后做这个检查:

        $("form").submit(function () {
            if ($(this).valid()) {  //<<< I was missing this check
                $("#loading").show();
            }
        });

这篇关于如果验证失败如何停止表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆