Amr Elnashar
Amr Elnashar

Reputation: 1769

How to make CheckBoxList Validator

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

Upvotes: 3

Views: 12402

Answers (5)

Homer
Homer

Reputation: 7806

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/

Upvotes: 10

Cesar Daniel
Cesar Daniel

Reputation: 737

I use the skmValidators to validate check boxes.

Creating Validator Controls for the CheckBox and CheckBoxList

Upvotes: 0

Philosopher
Philosopher

Reputation: 11

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

Upvotes: 1

Amr Elnashar
Amr Elnashar

Reputation: 1769

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;
            }
        }

Upvotes: 4

Pranay Rana
Pranay Rana

Reputation: 176896

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

Upvotes: 2

Related Questions