Related Links

Server Side CustomValidator not working

When I use the CustomValidate for server side validation, it doesn't seem to execute the server code. All my server code does (at the moment) is set args.IsValid = false; But everytime I run it, if I put anything in the textbox I'm validating, it passes.
I put a break in the server code and it doesn't break. I've set the "ControlToValidate" to the right text box.

Re: Server Side CustomValidator not working

If you have a button that should trigger validation try putting this code in the top of the event handler (for the button)

Page.Validate();

if (!Page.isValid) return;

Technically in most instances you don't need the Page.Validate() method. I've observed that sometimes UpdatePanels will result in validation not firing as expected and Page.Validate() seems to correct it. Also, in ASP.NET 1.1 there was an issue with
validation not firing with FireFox and some other browsers. That is when I developed the habit of including the Page.isValid check.

Re: Server Side CustomValidator not working

It appears there is a hierarchy in the Validation scheme. I found that the "RequiredFieldValidation" fields are evaluated and marked appropriately. If any fail the validation, it doesn't do the other Validations. Once all the RequiredFieldValidations are
done then it moves on to the other. Since I only use (at this time) required and customValidation I only know it doesn't do the custom until the required are complete.

Re: Server Side CustomValidator not working

Varnell

It appears there is a hierarchy in the Validation scheme. I found that the "RequiredFieldValidation" fields are evaluated and marked appropriately. If any fail the validation, it doesn't do the other Validations. Once all the RequiredFieldValidations are done
then it moves on to the other. Since I only use (at this time) required and customValidation I only know it doesn't do the custom until the required are complete.

I would like to clear up this misconception. Validators are evaluated in the order they appear in the Page.Validators collection. In fact, that collection is based on the order they appear in the control tree of the page. You may have had your RequiredFieldValidators
earlier on the page than others.

Re: Server Side CustomValidator not working

jaymehta_26

While you are using custom validator then you have to disable client side validation then after it will work.

I would like to clear up this misconception too. CustomValidators will operate fine whether client-side validation is supported or not. You can have EnableClientScript=true and the validator will have no effect on the client-side unless you assign the ClientValidationFunction
property. The key here is that you should always setup server side validation. Its already been mentioned. Make sure Page.Validate() runs (which happens when the button submits the page so long as its CausesValidation property is true). Your responsibility
is to test Page.IsValid is true in your button's Click event handler before trying to save or use the data from the page.

Re: Server Side CustomValidator not working

Re: Server Side CustomValidator not working

PLBlum

the validator will have no effect on the client-side unless you assign the ClientValidationFunction property.

Thanks, this was the key for me. I had all the pieces in place, but I missed the "ClientValidationFunction" property. Once I plopped the javascript client side validation funtion in there, it worked like charm.

I mean it worked okay before that, but it's nice to have all the errors show up at once, and not have to wait to clear the non-custom validation before seeing the custom validation errors.

Thanks for the great advice.

PLBlum

the validator will have no effect on the client-side unless you assign the ClientValidationFunction property.

Re: Server Side CustomValidator not working

FYI: If your trying to validate anything other than text boxes or any kind of input field and trying to validate a radio button list or any list controls using a customvalidator the
ValidateEmptyText property must be set to "True" for the validator to fire.

I like to use CustomValidators when I have mutiple list on a form and the user needs to select at least one item in at least one of the list. This way you can have one server validate function that checks that at least of the list has a selectedindex >
-1.