为什么jQuery Validation添加了noValidate属性? [英] Why is jQuery Validation adding noValidate attribute?

查看:433
本文介绍了为什么jQuery Validation添加了noValidate属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用:

<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script src="../js/bootStrap/js/bootstrap.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script>

经过验证,例如:

j$('.delValidate').each(function(){

j$(this).validate({
    rules:{
        delivery_Method:{
            required: true
        }
    },
    message:{
        delivery_Method:{
            required: true
        }
    }
});

});

    j$('.validateUser').each(function(){
        j$(this).validate({
            rules:{
                f_Name:{
                    required: true
                },
                l_Name:{
                    required: true  
                },
                username:{
                    required: true,
                    minlength: 6
                },
                password: {
                    validChars: true,
                    noSpace: true,
                    minlength: 6,
                    skip_or_fill_minimum: [3,".pw"]
                },
                confPass: {
                    equalTo: "#password",
                    skip_or_fill_minimum: [3,".pw"]
                }           
            }
        });
    });

HTML片段:

<form class="form-horizontal delValidate" action="#" method="post">
<input type="hidden" name="formIdentifier" value="addDel" />
<div class="form-group">
    <div class="col-sm-3">
        <input type="text" name="delivery_Method" class="form-control" placeholder="Enter new method" />
    </div>
    <div class="col-sm-2 col-sm-offset-2">
        <button type="submit"  class="btn btn-default btn-block">Add</button>
    </div>
</div>
</form>

但是,当我转到页面时,我的表单将使用属性noValidate呈现,因此我可以将null/empty值插入数据库中.这是什么原因呢? 是否必须以特定方式设置表格? jQuery验证是否选择了错误的内容,因此不起作用? 我的肮脏工作正在使用:

But when I go to my page, My form is rendered with the attribute noValidate and therefore I can insert null/empty values into the database. What is the reasoning for this? Does the form have to be set up in a particular way? Does jQuery validation pick up something is wrong and therefore doesn't work? My dirty work around is using:

j$(this).removeAttr('novalidate');

推荐答案

novalidate属性只是告诉浏览器禁用内置HTML5验证,或者忽略您可能使用的任何HTML5验证属性.

The novalidate attribute simply tells the browser to disable the built-in HTML5 validation, or ignore any HTML5 validation attributes you may have used.

jQuery Validate插件会动态添加novalidate属性,因为通过安装它,您已决定让该插件处理HTML5的验证 .

The jQuery Validate plugin dynamically adds the novalidate attribute because, by installing it, you've decided to let the plugin handle validation instead of HTML5.

JavaScript(jQuery)或HTML5这两种方法都不足以保护数据库. 当客户端验证失败,被用户/浏览器禁用或绕过客户端验证时,您应该始终使用某种服务器端验证.

Neither of those two methods, JavaScript (jQuery) or HTML5, are good enough for protecting your database. You should always have some kind of server-side validation in place for when client-side validation fails, is disabled, or bypassed by the user/browser.

引用OP:

我的表单使用属性noValidate呈现,因此我可以在数据库中插入null/empty值."

使用jQuery Validate插件的方式听起来很不对劲,并且novalidate属性不是根本原因.

Sounds like something is very wrong with the way you're using the jQuery Validate plugin, and the novalidate attribute is not the root cause.

如果不能输入null数据至关重要,则需要在服务器端提供数据验证代码来防止这种情况. 您绝不能依靠客户端代码来保护数据库,因为可以轻松地绕过所有客户端代码.

If it's critical that you cannot enter null data, you'll need data validation code on the server side to prevent this. You must never rely on client-side code to protect the database since all client-side code can be easily bypassed.

这是什么原因?"

如上所述.这是正常现象,也是所希望的.该插件仅使用novalidate来接管浏览器(HTML5)的客户端验证.

As explained above. This is normal and desired. The plugin simply uses novalidate to take over client side validation from the browser (HTML5).

如果没有HTML5验证属性,则novalidate属性或缺少novalidate属性绝对不会执行任何操作.换句话说,如果您使用HTML5验证属性,它只会切换HTML5验证.

If you have no HTML5 validation attributes, then the novalidate attribute, or lack of novalidate attribute, does absolutely nothing. In other words, it only toggles the HTML5 validation if you use HTML5 validation attributes.

是否必须以特定方式设置表格?"

是的.但是我们看不到您的标记来告诉您哪里出了错.

Yes. But we cannot see your markup to tell where you went wrong.

我的肮脏解决方法正在使用:.removeAttr('novalidate')"

"My dirty work around is using: .removeAttr('novalidate')"

这不明智,或者完全多余,具体取决于您的标记.实际上,您可能允许HTML5验证与jQuery Validate插件同时进行.选择一个或另一个进行客户端验证.

This is not smart, or completely superfluous, depending on your markup. Effectively, you are potentially allowing HTML5 validation to occur simultaneously with the jQuery Validate plugin. Pick one or the other for client-side validation.

编辑:

此后,OP已向该问题添加了一些HTML标记.

The OP has since added some HTML markup to the question.

jQuery Validate插件完全按预期工作: http://jsfiddle.net/tv7Bz/

The jQuery Validate plugin is working exactly as expected: http://jsfiddle.net/tv7Bz/

这篇关于为什么jQuery Validation添加了noValidate属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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