views:

1105

answers:

2

Hey,

If you use a CustomValidator control and a ValidationSummary control within an aspx page for server side validation you can set the ErrorMessage of the validator and its Text value will appear in place and the ErrorMessage value will be displayed within the ValidationSummary control.

Nice.

Now, what if I also perform validation on the client side in javascript, is it possible to perform the same kind of validation message display as if the server side code had fired? So that the ValidationSummary would display its usual HeaderText along with your additional ErrorMessage and the CustomValidator would display its Text value (e.g. *)

I can probably fake it for client side vs. server side but it would be nice to use the existing controls if at all possible.

Thanks in advance for any ideas or suggestions

A: 

I have tried getting at the DOM object for the Summary during runtime, but unless you have a very simple page, i.r. no AJAX controls like panel etc, you are out of luck.

callisto
+1  A: 

You can make the validator itself run on the client side when you set EnableClientScript to be true. ClientValidationFunction can then be set to the name of the javascript function that you write to validate the control. The rest of the CustomValidator control's behavior will tie in with the ValidationSummary just like any other validator, including putting the ErrorMessage up in the header, etc. etc.

Matt Hamsmith
This works very well, however it requires a postback (asynch or otherwise) anyway to make this happen via javascript events?For example if you wanted to validate a textbox onblur?
Adam Fox
If EnableClientScript is true, there is no postback. Here is the MSDN with an example: http://msdn.microsoft.com/en-us/library/f5db6z8k(VS.71).aspxYou can also do both (client-side and server-side), as the example illustrates. I also believe that validation happens by default when the textbox being validated loses focus (onblur).
Matt Hamsmith
Quite right, I did not have the ControlToValidate set. Thanks very much!
Adam Fox
@Adam - You are most welcome!
Matt Hamsmith
One slight issue. The client side validation if I click a button that has causesvalidation=true works exactly right, but the onblur for the textbox shows the * next it for the validator but the errormessage does not display within the validationsummary ... any ideas?
Adam Fox