How to validate a control that might be empty

  • Thread starter Thread starter Alan Silver
  • Start date Start date
A

Alan Silver

Hello,

I have a form in which the customer can enter their personal details.
Some customers will have an account number, in which case they are
allowed to enter this and not have to enter their address.

How do I do validation for this? They must enter one of account number
or address, but need not supply both.

AFAICS, validation controls only fire if the control to be validated
contains something, so if they don't enter an account number and don't
enter an address, validation won't happen.

Any ideas? TIA
 
How do I do validation for this? They must enter one of account number
or address, but need not supply both.

AFAICS, validation controls only fire if the control to be validated
contains something, so if they don't enter an account number and don't
enter an address, validation won't happen.

OK, I just found this which might be of use to others (watch out for URL
wrapping)...

http://groups.google.co.uk/group/microsoft.public.dotnet.framework.aspnet
/browse_frm/thread/cd27e867f2b65cfb/9c58c0ab63b1c7ee

Basically it seems that if you leave the ControlToValidate property
empty, then the validator always fires, even if the control is empty.

HTH
 
Demetri said:
It sounds like a job for the CustomValidator control.

Demetri and Winista,

A custom validator only fires if there is something in the control, that
was the point of my post. I know how to use the custom validator, but
was stuck with the problem that the user is required to enter one of two
pieces of information. If they don't enter either, the validator doesn't
fire. It doesn't matter what validator you use, it just doesn't fire.

I got around it partially by not setting the ControlToValidate property,
making the validator fire whenever a submit button is clicked. That
limited me to having to pull all the data to be validated inside the
function, meaning that I couldn't have a generic function that would do
the validation for several controls. This was a pain, but not the end of
the world.

I'd still like to know if there is a more elegant solution. Thanks
anyway.
 
Back
Top