jQuery验证最小长度规则不起作用 [英] jquery validate minlength rule is not working
问题描述
我有一个带有密码字段的表格.密码必须至少为8个字符.
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.
我做错了什么?我在其他页面上使用了相同的代码(JS部分),它按预期工作.
What am I doing wrong? I'm using the same code (the JS part) on other pages and it works as expected.
推荐答案
Validate查找输入字段的 name
,而不是id
.从rules
选项的文档中:
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.
示例: http://jsfiddle.net/kqczf/4/
这篇关于jQuery验证最小长度规则不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!