I have Checkboxlist and i can't give it required field validator or 开发者_开发技巧custom validator. it gives me runtime exception.
Language : Vb.net with asp.net
with jQuery and ASP.Net CustomValidator:
function validateCheckBoxList(sender, args) {
args.IsValid = ($("#CheckBoxListId :checked").length > 0);
}
<asp:CustomValidator ID="CustomValidator" runat="server" ErrorMessage="Required!" ClientValidationFunction="validateCheckBoxList"></asp:CustomValidator>
https://jsfiddle.net/t8qj4tqb/
Its working and here is the code
function CheckBoxListValidator(source, arguments) {
var Control;
Control = document.getElementById("CKlistVehicleBodies").getElementsByTagName("input");
var check = false;
if (eval(Control)) {
for (var i = 0; i < Control.length; i++) {
if (Control[i].tagName == 'INPUT') {
if (Control[i].checked) {
check = true;
}
}
}
if (!check)
arguments.IsValid = false;
else
arguments.IsValid = true;
}
}
no cannot apply required field validator on the checkbox list
but you can use custom validator to validate it
for the custom validator to work you have to create your own function on server or client side for validation and one more thing when you are using custom validator there no need to pass value in the controltovalidate
property
This one is FREE, comes with source code, and is just like the other .NET controls – drop it on a page, pick the checkbox list control to validate and you get client side and server side validation. It also works with AJAX. It even lets you pick a minimum and maximum number of checkboxes that have to be checked or can be checked.
http://www.aboutfortunate.com/Component-Library/Checkboxlist-Required-Field-Validator.aspx
I use the skmValidators to validate check boxes.
Creating Validator Controls for the CheckBox and CheckBoxList
精彩评论