Skip to main content

Validating checkbox list in asp.net using JQuery

ASP.net does not provide in built support to validate a checkbox list. We have to rely on custom javascript to verify if at least one checkbox in the list is checked.

Since we cannot add a validator to the checkbox list, I usually put a dummy textbox on the form and add a custom validator to that textbox.


<asp:CheckBoxList ID="cblLostDescription" runat="server" RepeatDirection="Horizontal" TabIndex="14">
<asp:ListItem Value="L">Lost/Stolen</asp:ListItem>
<asp:ListItem Value="D">Destroyed</asp:ListItem>
<asp:ListItem Value="M">Damaged</asp:ListItem>
<asp:ListItem Value="O">Other</asp:ListItem>
</asp:CheckBoxList>
<asp:CustomValidator ID="custValDescrption" runat="server" ErrorMessage="Property Description is required"
Display="Dynamic" ControlToValidate="txtPropertyDummy"
ClientValidationFunction="ValidatePropertyDescription" ValidateEmptyText="true"></asp:CustomValidator>
<asp:TextBox runat="server" ID="txtPropertyDummy" Width="1px" Style="display: none"></asp:TextBox>


Following is the client side validation function that we added to the custom validator. If we take a look at the rendered html, all The checkboxes in our list do not have the same id or name attribute.But all the ids start with the id we gave to the checkbox list. We can use JQeury to select the group of checkboxes whose ids start with a given text , then loop through the set and verify if at least one checkbox is checked. Make sure you have a reference to the JQuery script file on your asp.net page.


//Using JQuery, select all the checkboxes whose id begins with the
//asp.net checkbox list server control's ID.
function ValidatePropertyDescription(sender, args) {
var chkGroup = $("input[id^=<%=cblLostDescription.ClientID%>]");
//Loop through the set returned by JQuery
for (i = 0; i < chkGroup.length; i++) {
if (chkGroup[i].checked) {
args.IsValid = true;
return;
}
}
args.IsValid = false;
}

Comments

Popular posts from this blog

Find the cause of poor performance in Sql Server

I found the following two part article by Gail Shaw on Simple-Talk really helpful in trouble shooting poorly performing queries in Sql Server. The articles talks about spotting poorly performing queries with the help of the Profiler, understand Sql Server Query plans and fine tune the peformance using proper indexes. Part 1: http://tinyurl.com/ccl6gj Part 2: http://tinyurl.com/okcuqg

Kill a remote user session remotely

When trying to connect to your Windows 2000/2003 server remotely, you may receive the following error. "The terminal server has exceeded the maximum number of allowed connections." You could kill one or more of those connections by using PsExec tool that can be downloaded from the following link. This tool and a bunch of others were developed by SysInternals which was bought by Microsoft. http://www.microsoft.com/technet/sysinternals/utilities/pstools.mspx Open your command prompt and from the directory that contains the psexec utility, do the following 1) psexec \\x.x.x.x -u user -p password cmd (this will give you access to the cmd prompt on the server) Example: psexec \\127.0.0.1 -u admin -p password cmd 2) once you get the command prompt run the command qwinsta to get a list of all Terminal Services connections. Each connection has an Id Number. 3) Run the command logoff [id# of session to quit] /v (this will kill the connection with that id #) Example: logoff 2 /v Once ...

SharePoint: Comparing dates in XSLT

In one of my sharePoint projects, I had to compare a date with the current date in XSLT. As it turned out, there is no support for date comparision in XSLT. This is how I went about doing it. The trick involves converting the date string into a number Say the date is in ISO format YYYY-MM-DDTHH:MM:SSZ (for example, 2001-01-21T22:54:47Z). Say we have a variable DueDate <xsl:variable name="DueDate" select="'2001-01-21T22:54:47Z'"> Replace the dashes in the string with empty string Take the first 10 character in the date sring convert the string to number number(translate(substring(@DueDate,1,10),'-','')) On doing the above we get: 20010121 We can do the same thing with the date to compare. We can compare the duedate with the current date in SharePoint as follows number(translate(substring(@DueDate,1,10),'-','')) & lt; number(translate(substring(ddwrt:TodayIso(),1,10),'-','')) You can apply the same log...