views:

51

answers:

4

Is it possible to have "Required Field Validator" controls to validate more than one field (example I have 12 textboxes that are required. I want to try an avoid having 12 RFV controls. If a validation does get triggered, is there a way to display a customized message ("textA is empty" or "textB is empty") etc.?

+2  A: 

You can create a custom validator that goes through validates all the controls.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx

The Required Field Validator can only validate one control at a time.

Kevin
This is definitely an option, but don't set "ControlToValidate" or it may never fire.
Chris
A: 

You cannot do that with a RequiredFieldValidator; you could write your own CustomValidator to do that, but the validation would be on the server side rather than on the client side.

Paolo Tedesco
With CustomValidator, you can also register a client side script that should take care of the client side validation.
driis
In the CustomValidator, I cant seem to add more than one "ControlToValidate"
user279521
@driis: cool, I didn't know that..
Paolo Tedesco
@user279521 - It doesn't work like that. You do not set ControlToValidate to anything at all. In your server-side code, you check each field by name.
Chris
is it possible to add a customized error message when a validation does return "false" (such as "TextA is blank" or "TextB is blank") ?
user279521
Yup. If your CustomValidator is called cv, then just set cv.ErrorMessage to whatever you want.
Chris
A: 

Multiple Fields Validator - An ASP.NET Validation Control is what you need.

sashaeve
A: 

As mentioned by everyone else you can create your CustomValidator that can validate on the client side and on the server side. There are couple of things that you must keep in mind.

1) Make sure to expose your client script as a Web Resource. This will enable the script to be cached by the browser.

2) Use a certain attribute to target certain TextBoxes. This can be performed by giving them a certain class which will be validated in your Custom Validator control.

Hope it helps!

azamsharp