Click here to Skip to main content
13,254,366 members (57,160 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 7 Mar 2008

The AtLeastOneOfValidator Control

, 26 Mar 2008
Rate this:
Please Sign up or sign in to vote.
Use this validator control to require that at least one control in a list has a value.


ASP.NET includes a number of useful validation controls. Unfortunately, these controls have several limitations. For example, let's say you have several fields on your form and want to require the user to enter data in at least one of them. The AtleastOneOfValidator is built to solve that problem. Additionally, the control allows you to validate against the CheckBox, CheckBoxList, RadioButtonList, and Calendar controls. While the download includes the source code, this article is just to provide instructions on how to use the included binary.

Using the code

Begin by adding the control to your toolbox or <%@ Register/>-ing the assembly. If you need help with that, there are a number of other places already that will walk you through the process. Now, you can drop the control on your form, or create it declaratively, just like any other validator control.

In a normal validator, you would now set the ControlToValidate property, as well as any other properties you need. Since we will be validating several controls instead of just one, you should leave that property alone. In its place, AtLeastOneOfValidator provides ten other controls, named ControlToValidate0 through ControlToValidate9. You can set any of these in the same manner you would set ControlToValidate. The control will function no matter which order you specify your controls. You can even skip properties in the middle. Just remember that validation will happen in order, with the property number.

If you need to validate more than 10 controls, or if you want to set all the controls at once, there is also the ControlsToValidate property. This is just a string that accepts a comma-delimited list of control IDs. Note that this property replaces rather than supplements the other ten. If you set this in the designer after setting the others, you will over-write them. The reverse is also true. You should think of it as different ways of modifying the same value. If you edit the ASPX code directly, it's ControlsToValidate that really matters, and not the numbered properties.

Once these properties are set, it should work like any other validator. If any of the controls selected through these properties has a value, it will validate true. Otherwise, it will validate false. CheckBoxes and RadioButtons are deemed to have a value if they are checked. A Calendar has a value if any day or days are selected. Everything else has a value if the trimmed length of the value used for validation by a regular validator control is greater than 0.

Points of interest

Client-side validation works for everything but Calendar controls. If you include a Calendar control, it will have to post back to the page to finish validation. Since it's pretty much par for the course for the Calendar control to do a post back after every little change, this really isn't anything new.

I also want to thank Scott Mitchell for this article, without which it would not have been possible for me to write this control.

The control has been tested to work with IE7 and the latest Firefox 3 (beta 2 at the time of writing). I'm looking for people to test in Firefox 2 and older versions, IE6, the IE8 beta, Opera, Safari, Nautilus, and Camino. If you can put the control through its paces in any of those platforms, please post a comment and let me know how it goes.


This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralJavascript errors Pin
stribbed7-Jul-08 17:49
memberstribbed7-Jul-08 17:49 
GeneralRe: Javascript errors Pin
jcoehoorn8-Jul-08 4:13
memberjcoehoorn8-Jul-08 4:13 
GeneralRe: Javascript errors Pin
stribbed8-Jul-08 17:33
memberstribbed8-Jul-08 17:33 
GeneralRe: Javascript errors Pin
jcoehoorn9-Jul-08 3:51
memberjcoehoorn9-Jul-08 3:51 
GeneralMultipleFieldsValidator Pin
jcoehoorn11-Mar-08 11:06
memberjcoehoorn11-Mar-08 11:06 
GeneralRe: MultipleFieldsValidator Pin
Adam Tibi2-Jan-14 4:35
memberAdam Tibi2-Jan-14 4:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 26 Mar 2008
Article Copyright 2008 by jcoehoorn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid