Friday, May 23, 2014

How to make checkboxlist required on clientside using validator in ASP.Net?

You can do that by using CustomValidator.

Here is the simple code:

<script> 
function ValidateChkBxLst(source, args) {
var chkListModules = document.getElementById('<%= chkBxLst.ClientID%>'); var chkListinputs = chkListModules.getElementsByTagName("input"); for (var i = 0; i < chkListinputs.length; i++) { if (chkListinputs[i].checked) { args.IsValid = true; return; } } args.IsValid = false; } </script> <asp:checkboxlist id="chkBxLst" repeatcolumns="3" repeatdirection="Vertical" runat="server"/> <asp:customvalidator clientvalidationfunction="ValidateChkBxLst" errormessage="CheckBox List" id="cvList" runat="server" text="Please select at least one checkbox." />
Here I have created "CustomValidator" which is pointing to "ValidateChkBxLst" Javascript function to validate it clientside without postback. Isn't it simple?

Thanks

1 comment:

  1. Thanks for the helpful Post. You explained it very well

    ReplyDelete

React-select is very slow on larger list - Found solution - using react-window

 I had more than 4000 items in searchable dropdownlist. I have used react-select but it was very slow. finally I found complete solution to ...