jquery validate minlength rule is not working

戏子无情 提交于 2020-01-01 08:45:05

问题


I have a form with a password field. The password has to be at least 8 characters long.

<form action="/account/register" method="post" id="form-register">
<div class="input-row">
    <input name="data[User][password]" type="password" id="FUserPassword" class="required" placeholder="Password">
</div>

</form>

$("#form-register").validate({
        rules: {
            FUserPassword : {
                minlength: 8
            }
        }
    });

It applies the "required" rule, but it just ignores the "minlength" rule.

What am I doing wrong? I'm using the same code (the JS part) on other pages and it works as expected.


回答1:


Validate looks for the name of the input field, not the id. From the documentation for the rules option:

Key/value pairs defining custom rules. Key is the name of an element (or a group of checkboxes/radio buttons)

With that in mind, you should use something like this:

$("#form-register").validate({
    rules: {
        'data[User][password]': {
            minlength: 8
        }
    }
});

For input names with special characters, be sure to quote the object key.

Example: http://jsfiddle.net/kqczf/4/



来源:https://stackoverflow.com/questions/14404884/jquery-validate-minlength-rule-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!