jquery 复选框验证插件 - 至少选中一个
jquery validation plugin for checkbox- at least one selected
我正在使用 jquery 验证插件来验证表单字段。我在表单中有多个复选框。我为逻辑 'at least one need to be selected' 尝试了几种不同的方法,但没有任何效果,但是当我从验证插件中尝试一些东西时,它可以很好地处理这段代码。如果你能告诉我如何将下面的代码合并到验证插件中
<script type="text/javascript">
$(document).ready(function () {
$('.submit').click(function() {
checked = $("input[type=checkbox]:checked").length;
if(!checked) {
// <form:errors path="chk" cssClass="errors" element="div" />
$(".form-error").text("* You must check at least one checkbox.").show();
// alert("You must check at least one checkbox.");
return false;
}
});
});
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script>
$(document).ready(function(){
$("#myForm").validate({
rules: {
crcode: "required",
dtype: "required",
pview: "required",
prview: "required",
},
messages: {
crcode: "Rate code is required",
dtype: "Dwell type is required",
pview: "Public view is required",
prview: "Private view is required",
}
});
});
</script>
Here are my checkboxes
<div class="[ form-group ]" style="font-family: KlavikaWebBasicBold;" >
<input type="checkbox" name="chkbox" id="fancy-checkbox-default1" autocomplete="off" class="fancy" />
<div class="[ btn-group ]">
<label for="fancy-checkbox-default1" class="[ btn btn-default ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="fancy-checkbox-default1" class="[ btn btn-default active ]">
Video+Internet
</label>
</div>
</div>
<div class="[ form-group ]" style="font-family: KlavikaWebBasicBold;">
<input type="checkbox" name="chkbox" id="fancy-checkbox-default2" autocomplete="off" class="fancy" />
<div class="[ btn-group ]">
<label for="fancy-checkbox-default2" class="[ btn btn-default ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="fancy-checkbox-default2" class="[ btn btn-default active ]">
Video+Phone
</label>
</div>
</div>
<div class="[ form-group ]" style="font-family: KlavikaWebBasicBold;">
<input type="checkbox" name="chkbox" id="fancy-checkbox-default3" autocomplete="off" class="fancy" />
<div class="[ btn-group ]">
<label for="fancy-checkbox-default3" class="[ btn btn-default ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="fancy-checkbox-default3" class="[ btn btn-default active ]">
Video+Security
</label>
</div>
</div>
if you can tell me how I can incorporate this below code within validation plugin
$('.submit').click(function() {
checked = $("input[type=checkbox]:checked").length;
if (!checked) { ....
你不知道。
该代码与 jQuery 验证插件一起使用没有意义, 可以完全删除 。使用 jQuery 验证插件时,您不需要为复选框验证编写完全独立的函数,也不需要任何类型的点击处理程序。
您只需在复选框名称上声明 required
规则,插件将自动要求至少一个复选框。
$(document).ready(function(){
$("#myForm").validate({
rules: {
chkbox: "required", // <- add this
crcode: "required",
dtype: "required",
pview: "required",
prview: "required",
},
messages: {
chkbox: "* You must check at least one checkbox.", // <- add this
crcode: "Rate code is required",
dtype: "Dwell type is required",
pview: "Public view is required",
prview: "Private view is required",
}
});
});
演示:http://jsfiddle.net/42t2twLo/
可以使用 the errorPlacement
option 来帮助准确放置此复选框错误消息。
errorPlacement: function(error, element) {
if (element.is(":checkbox")) {
error.insertBefore(element); // custom placement example
} else {
error.insertAfter(element); // default placement
}
}
我正在使用 jquery 验证插件来验证表单字段。我在表单中有多个复选框。我为逻辑 'at least one need to be selected' 尝试了几种不同的方法,但没有任何效果,但是当我从验证插件中尝试一些东西时,它可以很好地处理这段代码。如果你能告诉我如何将下面的代码合并到验证插件中
<script type="text/javascript">
$(document).ready(function () {
$('.submit').click(function() {
checked = $("input[type=checkbox]:checked").length;
if(!checked) {
// <form:errors path="chk" cssClass="errors" element="div" />
$(".form-error").text("* You must check at least one checkbox.").show();
// alert("You must check at least one checkbox.");
return false;
}
});
});
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script>
$(document).ready(function(){
$("#myForm").validate({
rules: {
crcode: "required",
dtype: "required",
pview: "required",
prview: "required",
},
messages: {
crcode: "Rate code is required",
dtype: "Dwell type is required",
pview: "Public view is required",
prview: "Private view is required",
}
});
});
</script>
Here are my checkboxes
<div class="[ form-group ]" style="font-family: KlavikaWebBasicBold;" >
<input type="checkbox" name="chkbox" id="fancy-checkbox-default1" autocomplete="off" class="fancy" />
<div class="[ btn-group ]">
<label for="fancy-checkbox-default1" class="[ btn btn-default ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="fancy-checkbox-default1" class="[ btn btn-default active ]">
Video+Internet
</label>
</div>
</div>
<div class="[ form-group ]" style="font-family: KlavikaWebBasicBold;">
<input type="checkbox" name="chkbox" id="fancy-checkbox-default2" autocomplete="off" class="fancy" />
<div class="[ btn-group ]">
<label for="fancy-checkbox-default2" class="[ btn btn-default ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="fancy-checkbox-default2" class="[ btn btn-default active ]">
Video+Phone
</label>
</div>
</div>
<div class="[ form-group ]" style="font-family: KlavikaWebBasicBold;">
<input type="checkbox" name="chkbox" id="fancy-checkbox-default3" autocomplete="off" class="fancy" />
<div class="[ btn-group ]">
<label for="fancy-checkbox-default3" class="[ btn btn-default ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="fancy-checkbox-default3" class="[ btn btn-default active ]">
Video+Security
</label>
</div>
</div>
if you can tell me how I can incorporate this below code within validation plugin
$('.submit').click(function() {
checked = $("input[type=checkbox]:checked").length;
if (!checked) { ....
你不知道。
该代码与 jQuery 验证插件一起使用没有意义, 可以完全删除 。使用 jQuery 验证插件时,您不需要为复选框验证编写完全独立的函数,也不需要任何类型的点击处理程序。
您只需在复选框名称上声明 required
规则,插件将自动要求至少一个复选框。
$(document).ready(function(){
$("#myForm").validate({
rules: {
chkbox: "required", // <- add this
crcode: "required",
dtype: "required",
pview: "required",
prview: "required",
},
messages: {
chkbox: "* You must check at least one checkbox.", // <- add this
crcode: "Rate code is required",
dtype: "Dwell type is required",
pview: "Public view is required",
prview: "Private view is required",
}
});
});
演示:http://jsfiddle.net/42t2twLo/
可以使用 the errorPlacement
option 来帮助准确放置此复选框错误消息。
errorPlacement: function(error, element) {
if (element.is(":checkbox")) {
error.insertBefore(element); // custom placement example
} else {
error.insertAfter(element); // default placement
}
}