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

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

问题描述

更新: 我的问题更多地是关于How to prevent the form submit if the validation fails

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

链接无法解决我的问题

只需重申一下我在做什么: 我有一个带有一堆输入字段的表单,当用户单击提交"按钮时,如果表单验证失败,则使用属性required="required"验证表单.然后,我想停止提交表单,因为当用户看到JavaScript时,它会显示div提交表格.

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.

希望这可以清除.

END UPDATE

END UPDATE

如果表单验证失败,如何停止引发表单提交?

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天全站免登陆