2

This will be a two part questions:

  1. I have multiple textbox-es. All of them are required fields. How do I use ONE validator to validate ALL of the textboxes? (i.e. it will return error if any one of the textbox-es is empty)

  2. I have multiple textbox-es. AT LEAST one of them is/are required fields. How do I use ONE validator to validate such conditions? (i.e. it will return error if ALL of the textbox-es are empty, but will pass as long as one of this group of textbox-es has something in it.)

3 Answers 3

5

You need to use a Custom Validator (MSDN Link), as you will not be able to do what you want with the "single control" based validators.

This will allow you to write both client-side and server-side code, and respond accordingly.

It is very important that you implement server-side code, as client-side can easily be bypassed by somebody who knows what they're doing. Client-side is just a nice-to-have.

Sign up to request clarification or add additional context in comments.

4 Comments

Are you saying the Validation that comes in the toolbox are client side?
To the best of my knowledge, all the "other" validators (RequiredField, Range, RegularExpression, etc) will do both client-side and server-side for you (if the detected browser supports it). Because CustomValidator is written by yourself, it is your job to provide client-side (optional) and server-side (technically optional, but absolutely necessary) code
@C.J. think of it as client side as a helpful check for a user and the server side as the security check. The existing validators do both for you, you should do both with yours too.
@C.J. - Liath is correct, client-side just makes a nicer user experience (because a post-back is not necessary). Just in case you're not aware, you should also always check for Page.IsValid within your event handler as well, otherwise any validators that fail their check might be ignored
0

Set up a required field validator for each textbox and assign them to a validation group.You can use a ValidationSummary control to display all the results in a single list.

You can use a custom validator if you want to do your "one of these" logic. Unfortunately this will require you implement both the client side and server side validation code, there's no out the box validator.

3 Comments

I shiver ever time I see a reference to w3fools.com (See linked website for reasons)
@freefaller normally I'd agree but it was the highest ranked example outside SO. I've chosen a different one.
Unfortunately as they have reached the top, they continue to stay at the top... linking to them helps keep them there. I automatically skip over anything I see from them
0

I dont know of a way to do that, rather than create your own custom validation attribute. There are more than a few ways to achieve that, but the approach I would use is to assign the attribute to all included properties and then use reflection to check if at least one of them has a value (identifying them by checking if they have the attribute). In the IsValid method of the ValidationAttribute you have all the access to the instance and type so reflection should do the trick. Also, if you want client side validation to work, you can extend the IClientValidatable, send the values of all required fields through the GetClientValidationRules() rule parameters, and finally write your own javascript function to execute the rule. It sounds as an awful lot of work and it is, but if you make it flexible enough it should be reusable. Adding and removing the attribute to a property in a class should include/exclude a field from the validation list (if I may call it like that).

Here is where I got more familiar with that stuff when I had similar issues: Conditional Validation in ASP MVC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.