How to make a CheckBoxList Validator

I have a Checkboxlist, and I cannot provide it with a required authentication module or a custom validator. this gives me an exception at runtime.

Language: Vb.net with asp.net

+3
source share
5 answers

no cannot apply the required field validator in the checkbox list

but you can use a special validator to test it

for the custom validator to work, you need to create your own function on the server or on the client side for verification, and one more thing when you use the custom validator is there is no need to pass the value in the property controltovalidate

+2
source

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/

+10

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;
            }
        }
+4
0

Source: https://habr.com/ru/post/1747063/


All Articles