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
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
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.
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.