Feed on
Posts
Comments

During a class today someone asked me how to validate multiple ASP.NET controls.  The framework has the CustomValidator class that can handle this scenario.  Unlike all the other validation controls the CustomValidator does NOT need to have the ControlToValidate property set to an existing control.  If you need to determine validation based on multiple controls (e.g. one of two textboxes must contain text) this is the solution.

From the MSDN help.

It is possible to use a CustomValidator control without setting the ControlToValidate property. This is commonly done when you are validating multiple input controls or validating input controls that cannot be used with validation controls, such as the CheckBox control. In this case, the Value property of the arguments parameter passed to the event handler for the ServerValidate event and to the client-side validation function always contains an empty string (""). However, these validation functions are still called, where appropriate, to determine validity on both the server and client. To access the value to validate, you must programmatically reference the input control you want to validate and then retrieve the value from the appropriate property. For example, to validate a CheckBox control on the server, do not set the ControlToValidate property of the validation control and use the following code for the handler for the ServerValidate event

   1: Sub ServerValidation (source As object, args As ServerValidateEventArgs)
   2:    args.IsValid = (CheckBox1.Checked = True)
   3: End Sub

 

 

Comments

re: TIP: Validating Multiple ASP.Net controls 1/16/2006 7:25 PM Neal Davis

Great explanation on the server-side implementation, but can you show how to programmatically access a control’s value on the client using JavaScript?


Leave a Reply