How to validate checkbox list using client side in SharePoint

In this SharePoint tutorial, we will discuss how to validate checkbox list using client side in SharePoint.

Validate checkbox list using client side in SharePoint

As we know we are using server-side control CheckBoxList to show the group of checkboxes with single selection or Multi Selection.

So here I am binding this checklist box using SharePoint server-side code in my page load.

 <asp:CheckBoxList ID="chkPropertyContains" runat="server" RepeatLayout="OrderedList" Width="432px">                                                           
                            </asp:CheckBoxList>  
                                 <asp:CustomValidator ValidationGroup="vgForm" ID="CustomPropertyName" runat="server"
                                Display="None" ErrorMessage="<%$Resources:cvPropertName %>" ClientValidationFunction="PropertyName_clientValidate">
                            </asp:CustomValidator>
validate checkbox list using client side in SharePoint

Below is the code to bind the value in CheckListBox control in page load. You have to write a query to get the data from the table and bind it in CheckListBox control along with you have to set text field and value field.

myFacade = Factory.GetFacade<IcustomFacade>();
                    chkPropertyContains.DataSource = IcustomFacade.GetPropertyContains();
                    chkPropertyContains.DataTextField = "PropertyContains";
                    chkPropertyContains.DataValueField = "PropertyContainsEn";
                    chkPropertyContains.DataBind();

Next we have to validate CheckListBox checked property. Means it should have one check, then you have to add the below code.

function PropertyName_clientValidate(source, args) {
       
       var checkBoxList =document.getElementById("<%=chkPropertyContains.ClientID %>");
        var checkboxes = checkBoxList.getElementsByTagName("input");
        var isValid = false;
        for (var i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked) {
                isValid = true;
                break;
            }
        }
        args.IsValid = isValid;
    }
validate checkbox list using client side in SharePoint

This below code we can apply to check or on-check the checkbox . If you check “none” then automatically other two checkbox are on-check and “none” checkbox will be on-check, if you check any of the other values.

$('#<%= chkPropertyContains.ClientID %>').change(function () {
        var isNoneSelected = false;
        var selectPropertyContains = $("#<%= chkPropertyContains.ClientID %> input:checked").val();
        $("#<%= chkPropertyContains.ClientID %> input:checked").each(function () {
            var id = $(this).attr("value");
            if (id == 'None') {
                isNoneSelected = true;
            }
        });

        if (isNoneSelected) {
            $('#<%=chkPropertyContains.ClientID %>').find("input[value='Agricultural Garden']").prop("checked", false);
            $('#<%=chkPropertyContains.ClientID %>').find("input[value='Swimming Pool']").prop("checked", false);
        }
       

    });
validate checkbox list using client side in SharePoint

Source code to Insert the CheckBox value with Multi Selection in SharePoint List

SPFieldMultiChoiceValue values = new SPFieldMultiChoiceValue();

                    for (int i = 0; i < chkPropertyContains.Items.Count; i++)
                    {
                        if (chkPropertyContains.Items[i].Selected == true)
                        {
                             values.Add(chkPropertyContains.Items[i].Value.ToString()); 

                        }
                    }
 item["propertyContain"] = PropertyContains;

The above code we can use to insert the Multi selection checkbox data in SharePoint List.

You may like following SharePoint object model tutorials:

In this tutorial, we learned how to validate checkbox list using client side in SharePoint.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>