Geeks With Blogs
urig Tidbits from a .net life

For years I've been living with the unsettling feeling that there's something mysteriously wrong with ASP.Net's CustomValidator control. Every time I made the courageous decision to tackle this elusive control and try to harness it for my use, something would go wrong. Something I couldn't quite put my finger on until now.

I've just discovered the terrible secret behind the CustomValidator control: it doesn't fire if the control to be validated has an empty value. Here it is, in black on white, from the MSDN Library:

Note If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control.

WTF?! Who ever came up with *that* constraint? And why did I take so long to find this out? (answer: I should have been more patient and read the documentation through.)

The problem is that it's easy to imagine a scenario where checking whether a control is empty is an essential part of a more complex logic which warrants a CustomValidator. What then?

In my current scenario, I attached the CustomValidator to a TextBox to see whether it was empty or not, in conjuction with checking whether a DropDownList had a specific value. When I did that, the CustomValidator simply would not fire - not on the client nor on the server. Luckily for me, I was able to attach to the DropDownList instead of the TextBox and then the control would fire properly.

How did some of you overcome this limitation of ASP.Net's CustomValidator? Posted on Thursday, July 27, 2006 3:17 PM Knowledge Base , Developer Tools | Back to top


Comments on this post: ASP.Net CustomValidator is useless for empty controls

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
I did it the same way you recommended, setting the ControlToValidate to a control that will never be blank.

I totally agree this should not be a requirement for the validation to fire. A CustomValidator is used when the other out-of-the-box validator do not serve the requirements for validation, such is the case in a complex validation scenario (multiple control values and combinations checked.)
Left by T on Sep 08, 2006 3:07 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
What do you make of Microsoft resolving this issue in ASP.Net 2.0 but not in 1.1? (See my latest post on the subject)
Left by urig on Sep 19, 2006 1:36 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
Wow, thanks for that info! Although most of my code's moved on to 2.0, I still have some crucial apps in 1.1 and this has been driving me NUTS! I thought I was losing my mind, because all my other custom validators were firing.

Because of 1.1's ungraceful postback behavior (on a long scrolling form it's hell when you do an AutoPostback on a radiobuttonlist), I've had to use JavaScript to disable certain fields based on the radio button. But as long as the field is visible, the RequiredFieldValidator fires, even when that field is disabled and I don't want a value in it. So my solution was to implement the required field check as a CustomValidator, except it never fires!!!

I probably should have done a Google before I started this, but it just seems insane that this is how it works.

Thanks again!!!
Left by Serge van Neck on Feb 14, 2007 5:30 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
well, your site is useless in IE...incase you didn't know.
Left by yeah on Oct 25, 2007 3:15 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
What makes you say that? Looks fine to me. (I'm using
IE7 on PC)
Left by urig on Oct 25, 2007 4:13 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
This drove me nuts in ASP.NET 1.1, until I discovered that If you want the validator to always be fired....you just have to avoid using the ControlToValidate property in the aspx page...that way the validator will always be fired.

So if you have the controlToValidate property set in your customvalidator just erase it, and you'll see how everything works.

I will soon publish an article on this on www.howtoprogramsolutions.com
Left by David on Jan 24, 2008 4:28 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
Thanks Toto for the ValidateEmptyText="true" tip (definitely works in 2.0, don't know about 1.1).
Left by Adrian on May 16, 2008 12:43 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
SOLUTION !!!

Use the custom validator but do not set a ControlToValidate.
This makes the validator fire everytime the form is submitted!!
On the validation procedure, test sender.Id to find out what validation has been triggered !

I hope that helps...
Left by Fernando Paiva on Jul 23, 2008 5:56 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
You are wrong.

<asp:CustomValidator runat="server" ID="cMedicalHistoryDate" ErrorMessage="Medical history date"
Display="Dynamic" ValidateEmptyText="true" Text="*" ControlToValidate="tbMedicalHistoryDate" ClientValidationFunction="DateRequired" />


<script type="text/javascript">

function DateRequired(sender, args)
{
args.IsValid = false;
}

</script>
Left by Mike Flynn on Jan 20, 2009 7:45 AM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
In ASP.NET 2.0, there is property call "ValidateEmptyText" for CustomValidator.
If you set it to "true", validation event will fire event if the control to validate is empty :) otherwise, you're right nothing appends.
Left by _webFlo on Feb 13, 2009 4:32 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
You get this in 3.5, not sure how far back it goes though:

myValidator.ValidateEmptyText = true;
Left by Mikey on Apr 29, 2009 6:16 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
This is not the best solution but if all else fails, you can use javascript to call that validator... ValidatorValidate(val)

For example:
Set the OnClientClick attribute to a js function that will call the validator explicitly.

1. set the submit button's OnClientClick="return callCustomValidator()"

2. create the js function callCustomValidator().

In this function, you will call

ValidatorValidate(<%= myCustomValidator.ClientID %>);
if (<%= myCustomValidator.ClientID %>.isvalid) {
return true;
} else {
return false;
}

If this function return false, then postback will not occur.


Left by twogiraffe on Jul 24, 2009 6:46 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
You can also use the onblur attribute to explicitly call the javascript

ValidatorValidate(<%= myValidator.ClientID %>)
Left by twogiraffe on Jul 24, 2009 7:02 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
wow I just find this discussion when I started to have same issues in an application now and its a year since this discussion but I was having the same issue over and over...What David said works like charm...

The Custom Validator was working fine with a dropdown list and a required field textbox in Firefox, but when I tried in IE 7, it was not firing!
I took out the controltovalidate from the customvalidator and it now works in both...Im glad I found this discussion
Thank you!
Yanira
Left by Yani on Aug 29, 2009 8:34 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
Give me a break already. Just reverse the IDs in the ControlToCompare and ControlToValidate properties and your email example will work fine. The validator is not "broken".
Left by boo on Oct 01, 2009 2:29 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
I've just gone through this rigmarole myself of scratching my head as to why it wasn't working when the value was null - this was even after reading the important fact on MSDN! - I put it down to working at 4am. Anyhow, as someone else mentioned, the solution is simple:

Decouple the CustomValidationControl from any one specific control (this attribute is optional) - this way, the CustomValidationControl always fires. Then, within your event handler for the control, just manually target the form input field you wish to validate against.
Left by Suraj on Nov 26, 2009 10:56 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
This should be renamed to ASP.Net CustomValidator is useless. I have a custom validator that does not fire under any circumstances. I have tried all the fixes like removing controltovalidate, forcing validation etc.

It seems to me that the ONLY way to do custom validation is with code on the "Submit" Button, setting page.isvalid = false and then doing a postback.

Why bother putting somethiing in there that clearly doesn't work.
Left by Peter K on Dec 13, 2009 8:02 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
Did you realise you can apply more than one validator to a control? You can point a RequiredFieldValidator and a CustomValidator to a single control and check for both your requirements.
Left by rtpHarry on Dec 21, 2009 7:14 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
Set ValidateEmptyText to true and then all should work fine.
Left by Avinash on Aug 10, 2010 9:13 AM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
set ValidateEmptyText="true"
Left by Iurii on Oct 22, 2010 5:11 PM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
CustomValidator has property ValidateEmptyText="true" in asp 2.0
Left by Sergey on Jan 28, 2011 4:08 AM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
This doesn't seem horribly unreasonable. If a field is required, you need a RequiredFieldValidator. That's easy to do. If it's required AND it has other validation requirements, you add the validators you need to meet those reqirements, whether they be checking the format (RegularExpressionValidator), whether it's in a particular range (RangeValidator) or some other custom logic. That's where the CustomValidator comes in.

By allowing you to mix and match, you can pretty easily deal with the situation where a field is optional (don't use the RequiredFieldValidator), but if the user enters some value it must meet some requirement. If the CustomValidator always fired, you would need to write code to deal with blank or empty values.

Left by Dave on Apr 30, 2011 9:36 AM

# re: ASP.Net CustomValidator is useless for empty controls
Requesting Gravatar...
Agreed, I've been wrestling with this for several hours only to discover you need *two* validators: one for empty text, one for custom validation. Like you, I had a "WTF" moment.

I decided screw it, I moved all validation to the server side. Yes it necessitates a postback trip but you have all your validation code in one spot, written in easy-to-understand C# or VB. No flaky ASP controls, no clunky JavaScript, and a nice IDE with Intellisense.
Left by Geoff Valer on May 03, 2011 1:07 PM

Your comment:
 (will show your gravatar)


Copyright © urig | Powered by: GeeksWithBlogs.net