CheckboxList required field validation


Today I came across a situation where I need to validate CheckboxList as required field and found below solution:

//Bound list from Database
<asp:CheckBoxList ID="chlZones" runat="server" RepeatColumns="5" 
RepeatDirection="Horizontal" />
//Create Custom validator that will work as required validator. Profile function
<asp:CustomValidator ID="cvalZones" runat="server" 
ClientValidationFunction="ValidateZones" ErrorMessage="!!!" 
Display="Dynamic" />

//Implement Client method
<script>
function ValidateZones(source, args) {
		var chlZones = document.getElementById('');
		var chkLista = chlZones.getElementsByTagName("input");
		for (var i = 0; i < chkLista.length; i++) {
			if (chkLista[i].checked) {
				args.IsValid = true;
				return;
			}
		}
		args.IsValid = false;
	}
</script>
Advertisements
Tagged with: ,
Posted in Asp.Net
8 comments on “CheckboxList required field validation
  1. Arun says:

    good one 🙂

  2. Philosopher says:

    This one is FREE, comes with source code, and is just like the other .NET controls – drop it on a page, pick the checkbox list control to validate and you get client side and server side validation. It also works with AJAX. It even lets you pick a minimum and maximum number of checkboxes that have to be checked or can be checked.

    http://www.aboutfortunate.com/Component-Library/Checkboxlist-Required-Field-Validator.aspx

  3. bas says:

    working fine while unchecked the checklist box items..but after checked also it continously showing this validation..plz correct it

  4. Hi,

    This is very informative article. Thanks for sharing your knowledge. There are few links that also helpful for developers. This article have described to validate CheckBox, CheckBoxList, DropDownList, FileUpload, RadioButton, RadioButtonList, TextBox using jquery.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Top Rated

Error: Please make sure the Twitter account is public.

Blog Stats
  • 30,188 hits
%d bloggers like this: