Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Checkboxlist - validation - client side javascript (http://www.velocityreviews.com/forums/t103527-checkboxlist-validation-client-side-javascript.html)

=?Utf-8?B?U2FuZHk=?= 05-27-2005 03:56 PM

Checkboxlist - validation - client side javascript
 
Hello!

I need client side script to verify that at least one checkbox is checked in
a checkboxlist. Any suggestions?

--
Sandy

prasad 11-13-2006 04:15 AM

hello

I need client side script to verify that at least one checkbox is checked in
a checkboxlist. Any suggestions?

--
------------------------------------------------------------------------------------------------------------------------------------------------------

ankur.d.1987 10-28-2011 09:17 AM

Validate Checkbox List using client side java-script
 
1 Attachment(s)
Hi

You can use simple javascript to validate a check box list follows:

I have written two javascript functions for this:-

1) ValidateEducations() : This function finds the checkboxlist control in the client side and iterates through all the listitems of the checkboxlist. It takes up every listitem (which is actually a checkbox) one by one and finds whether it is checked or not. The moment it finds a checked checkbox it returns true indicating that the checkboxlist is validated. If none of the listitems(child checkboxes) are found checked and the for loop (enumerating through the listitems) ends, it returns false indicates that checkboxlist validation failed.

2) ShowHidevalidationError(): This function just does the job of showing/hiding validation error.

/******************************/
<script type="text/javascript" language="javascript">
function ValidateEducations() {
var chkEducations = document.getElementById('<%= chkEducations.ClientID %>');
if(chkEducations) {
var chkEducationsItemList = chkEducations.getElementsByTagName('input');
if(chkEducationsItemList) {
if(chkEducationsItemList.length > 0) {
for(var i = 0 ; i < chkEducationsItemList.length; i++) {
var chkEducationsItem = chkEducationsItemList[i];
if(chkEducationsItem) {
if(chkEducationsItem.type == 'checkbox') {
if(chkEducationsItem.checked) {
return ShowHideValidationError(false);
}
}
}
}
}
}
}
return ShowHideValidationError(true);
}

function ShowHideValidationError(show) {
var lblEducationsValidationError = document.getElementById('<%= lblEducationsValidationError.ClientID %>');
if(lblEducationsValidationError) {
if(show) {
lblEducationsValidationError.style.display = 'block';
return false;
} else {
lblEducationsValidationError.style.display = 'none';
return true;
}
}
}
</script>
/******************************/



And here goes the related HTML markup:
/************************************************** ********/
<table>
<tr>
<td>
<div>
<asp:Label ID="lblEducation" runat="server" Text="Education" />
</div>
<div>
<asp:Label ID="lblHint" runat="server" Text="(Please select all qualificatins you have acquired)"
Font-Size="8" ForeColor="GrayText" />
</div>
</td>
<td>
<asp:CheckBoxList ID="chkEducations" runat="server" RepeatDirection="Vertical" RepeatLayout="Flow"
TextAlign="Right" onclick="ValidateEducations()">
<asp:ListItem Text="Primary School" Value="1" />
<asp:ListItem Text="High School" Value="2" />
<asp:ListItem Text="Junior College" Value="3" />
<asp:ListItem Text="Graduate" Value="4" />
<asp:ListItem Text="Post Graduate" Value="5" />
<asp:ListItem Text="Phd" Value="6" />
</asp:CheckBoxList>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblEducationsValidationError" runat="server" Font-Bold="true" ForeColor="Red"
Text="Please select at least one educational qualification." Style="display:none" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmitForm" runat="server" Text="Submit" OnClientClick="return ValidateEducations()"
OnClick="btnSubmitForm_Clicked" />
</td>
</tr>
</table>
/************************************************** ********/



Description:
The checkbox list control renders as an HTML table if the RepeatLayout property is set as "table" and renders as HTML <span> if the RepeatLayout property is set as ""Flow".
In both he cases the client side javascript event onclick works. Hence the moment you check/uncheck a checkbox the validation functions will be called and the checkboxlist will be validated on the client side itself.

How do we validate?

Whether the checkboxlist renders into <span> or <table> it is definitely going to contain a collection of child HTML elements i.e. <input> tags with their type set to "checkbox" each.

This collection is retrieved by calling the method getElementsByTagName('input') on the instance of the checkboxlist control.

We then iterate through all the elements in this collection and for each element (checkbox) we assess the checked status. If checkbox is checked we return immediately because validation has succeeded. If we are, however, done iterating through all the elements (checkboxes) and still found none as checked then we display the validation failed message.


Thank you
Ankur


All times are GMT. The time now is 08:41 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57