使用 jquery 验证来验证数组输入的长度
Validating length of array inputs using jquery validation
一开始找了很多都没有文章解决我的问题
This article 让我知道使用引号来验证数组输入。
我的问题是如何验证数组输入的长度。更多详情,请阅读下文
在我的HTML:
<form id="demo-form">
<input type="text" name="product[]" id="p1">
<input type="text" name="product[]" id="p2">
<input type="text" name="product[]" id="p3">
<input type="text" name="product[]" id="p4">
<input type="submit value="submit">
</form>
这要求用户必须输入至少 2 个产品
脚本
jQuery("#demo-form").validate({
rules: {
'product[]': {
required: true,
minlength: 2 //this only use to validate length of string inputed, not length of array products
}
},
});
如何验证数组乘积的长度?
谢谢
每个文本输入 必须 具有唯一的名称,以便此插件正常运行。使用product[0]
、product[1]
、product[2]
等
使用 the require_from_group
method 确保至少填写两个字段。
使用the groups
option将四个相同的错误消息合并为一个。
示例:
jQuery("#demo-form").validate({
// other rules & options
groups: {
myproducts: "product[0] product[1] product[2] product[3]"
}
});
// assign the rule to all field names that start with 'product'
jQuery('[name^="product"]').each(function() {
jQuery(this).rules('add', {
require_from_group: [2, jQuery('[name^="product"]')]
});
});
一开始找了很多都没有文章解决我的问题
This article 让我知道使用引号来验证数组输入。
我的问题是如何验证数组输入的长度。更多详情,请阅读下文
在我的HTML:
<form id="demo-form">
<input type="text" name="product[]" id="p1">
<input type="text" name="product[]" id="p2">
<input type="text" name="product[]" id="p3">
<input type="text" name="product[]" id="p4">
<input type="submit value="submit">
</form>
这要求用户必须输入至少 2 个产品
脚本
jQuery("#demo-form").validate({
rules: {
'product[]': {
required: true,
minlength: 2 //this only use to validate length of string inputed, not length of array products
}
},
});
如何验证数组乘积的长度?
谢谢
每个文本输入 必须 具有唯一的名称,以便此插件正常运行。使用
product[0]
、product[1]
、product[2]
等使用 the
require_from_group
method 确保至少填写两个字段。使用the
groups
option将四个相同的错误消息合并为一个。
示例:
jQuery("#demo-form").validate({
// other rules & options
groups: {
myproducts: "product[0] product[1] product[2] product[3]"
}
});
// assign the rule to all field names that start with 'product'
jQuery('[name^="product"]').each(function() {
jQuery(this).rules('add', {
require_from_group: [2, jQuery('[name^="product"]')]
});
});