Saturday, March 7, 2015

ASP.Net Validating CheckBoxList with JavaScript

[sourcecode language="csharp"]

<asp:CheckBoxList ID="groupsList" runat="server" DataSourceID="GroupsDS" DataTextField="Name" DataValueField="Id" CssClass="span6 m-wrap groupsList" RepeatLayout="UnorderedList"></asp:CheckBoxList>
<asp:CustomValidator ID="CustomValidator1" ErrorMessage="Please select at least one group."
ForeColor="Red" ClientValidationFunction="ValidateCheckBoxList" runat="server" />
<asp:SqlDataSource runat="server" ID="GroupsDS" ConnectionString='<%$ ConnectionStrings:flexi_stocky %>' SelectCommand="SELECT * FROM [Groups]"></asp:SqlDataSource>

[/sourcecode]


[sourcecode language="javascript"]

<script type="text/javascript">
function ValidateCheckBoxList(sender, args) {
var checkBoxList = document.getElementById("<%=groupsList.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;
}

</script>

[/sourcecode]

No comments:

Post a Comment

How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...