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.
What am I missing? What other properties need setting for it to work?
I'm new to ASP and VS2005 so I'm probably missing something.
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.
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.
Thanks for the help, you got me off dead center and moving.
1. The ServerValidate event is not attached to your event handler method.
2. The control is Enabled=false or Visible=false
3. The control is contained in another control whose Visible property is false.
4. The validation group feature is being used. The validation group name on the button differs from the value in the Validator. (This is a common source of the problem)
5. If the textbox is blank, it will not fire unless ValidateEmptyText is true.
6. The button submitting the page has CausesValidation=false. This will prevent all validators from being evaluated on the server side unless you explicitly call Page.Validate()
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
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.
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
Marked as answer by Varnell on Feb 07, 2008 05:31 PM
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.
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
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.
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.
Varnell
Member
3 Points
12 Posts
Server Side CustomValidator not working
Feb 06, 2008 10:01 PM|LINK
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.
What am I missing? What other properties need setting for it to work?
I'm new to ASP and VS2005 so I'm probably missing something.
Thanks
dotnetbohn
Member
635 Points
119 Posts
Re: Server Side CustomValidator not working
Feb 07, 2008 01:49 AM|LINK
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.
Hope this helps.
Blog: http://www.silverlightconnection.com/
Varnell
Member
3 Points
12 Posts
Re: Server Side CustomValidator not working
Feb 07, 2008 02:14 PM|LINK
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.
Thanks for the help, you got me off dead center and moving.
jaymehta_26
Member
130 Points
49 Posts
Re: Server Side CustomValidator not working
Feb 07, 2008 04:43 PM|LINK
While you are using custom validator then you have to disable client side validation then after it will work.
if this is your answer then please mark as answer, thank you.
PLBlum
All-Star
30399 Points
5347 Posts
MVP
Re: Server Side CustomValidator not working
Feb 07, 2008 05:25 PM|LINK
There are several reasons it may not fire:
1. The ServerValidate event is not attached to your event handler method.
2. The control is Enabled=false or Visible=false
3. The control is contained in another control whose Visible property is false.
4. The validation group feature is being used. The validation group name on the button differs from the value in the Validator. (This is a common source of the problem)
5. If the textbox is blank, it will not fire unless ValidateEmptyText is true.
6. The button submitting the page has CausesValidation=false. This will prevent all validators from being evaluated on the server side unless you explicitly call Page.Validate()
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
PLBlum
All-Star
30399 Points
5347 Posts
MVP
Re: Server Side CustomValidator not working
Feb 07, 2008 05:27 PM|LINK
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.
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
PLBlum
All-Star
30399 Points
5347 Posts
MVP
Re: Server Side CustomValidator not working
Feb 07, 2008 05:29 PM|LINK
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.
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
akhil_csit
Member
2 Points
1 Post
Re: Server Side CustomValidator not working
May 09, 2008 08:59 PM|LINK
Thanx a lot. It worked for me right away.
bobeau78
Member
2 Points
2 Posts
Re: Server Side CustomValidator not working
Jan 04, 2010 04:38 AM|LINK
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.
custom validator custom validation
kgrissom
Member
27 Points
6 Posts
Re: Server Side CustomValidator not working
Oct 08, 2010 08:11 PM|LINK
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.