jQuery 验证隐藏字段
jQuery Validate hidden field
我试图在通过 Jquery 验证程序提交表单时验证元素的数量。原因是我们希望至少显示一张图片,但我们不直接上传它们,而是先显示预览。所以我需要以某种方式计算它们并验证至少存在一个。
我试过使用隐藏字段 as described here 但它根本不起作用。测试字段验证但由于某种原因我自己的规则甚至没有被触发。我的实现有什么问题?
隐藏字段技巧不再起作用,它似乎不再验证隐藏输入。
<form method="post" accept-charset="utf-8" id="submit-work-form" role="form" action="/" novalidate="novalidate">
<div class="form-group">
<input type="text" required="true" name="title"/>
<input type="hidden" name="image_count" required="required" data-rule-validcmsimagecount="true" aria-required="true">
<div class="photos">
<!-- containers go here -->
</div>
</div>
<div class="submit"><input type="submit" class="btn btn-black btn-default" value="Submit"></div>
</form>
JS:
$.validator.addMethod("validcmsimagecount", function(value, element) {
alert('Doesn\'t work');
return $('.photo-container').length > 0;
}, "You must add at least one image.");
$('#submit-work-form').validate({
submitHandler: function (form) {
form.submit();
}
});
您也可以尝试 Jsfiddle。
这可能是 this post but since I updated your fiddle to fix it, I thought I would post is as an answer. Looks like you need to enable the validation of hidden fields and remove the required
attribute from the hidden field. Once done, the validation will run, see the updated fiddle here 的副本,主要变化是添加了 ignore
属性:
$('#submit-work-form').validate({
ignore: [],
submitHandler: function (form) {
form.submit();
}
});
Jquery 验证默认忽略隐藏字段,您可以通过如下设置忽略选项强制验证隐藏字段
$('#myform').validate({
ignore: [],
// any other options and/or rules
});
jQuery Validate - Enable validation for hidden fields
这是针对您的问题修改后的示例
https://jsfiddle.net/9hoa1mrd/3/
我试图在通过 Jquery 验证程序提交表单时验证元素的数量。原因是我们希望至少显示一张图片,但我们不直接上传它们,而是先显示预览。所以我需要以某种方式计算它们并验证至少存在一个。
我试过使用隐藏字段 as described here 但它根本不起作用。测试字段验证但由于某种原因我自己的规则甚至没有被触发。我的实现有什么问题?
隐藏字段技巧不再起作用,它似乎不再验证隐藏输入。
<form method="post" accept-charset="utf-8" id="submit-work-form" role="form" action="/" novalidate="novalidate">
<div class="form-group">
<input type="text" required="true" name="title"/>
<input type="hidden" name="image_count" required="required" data-rule-validcmsimagecount="true" aria-required="true">
<div class="photos">
<!-- containers go here -->
</div>
</div>
<div class="submit"><input type="submit" class="btn btn-black btn-default" value="Submit"></div>
</form>
JS:
$.validator.addMethod("validcmsimagecount", function(value, element) {
alert('Doesn\'t work');
return $('.photo-container').length > 0;
}, "You must add at least one image.");
$('#submit-work-form').validate({
submitHandler: function (form) {
form.submit();
}
});
您也可以尝试 Jsfiddle。
这可能是 this post but since I updated your fiddle to fix it, I thought I would post is as an answer. Looks like you need to enable the validation of hidden fields and remove the required
attribute from the hidden field. Once done, the validation will run, see the updated fiddle here 的副本,主要变化是添加了 ignore
属性:
$('#submit-work-form').validate({
ignore: [],
submitHandler: function (form) {
form.submit();
}
});
Jquery 验证默认忽略隐藏字段,您可以通过如下设置忽略选项强制验证隐藏字段
$('#myform').validate({
ignore: [],
// any other options and/or rules
});
jQuery Validate - Enable validation for hidden fields
这是针对您的问题修改后的示例 https://jsfiddle.net/9hoa1mrd/3/