How to validate a control that might be empty

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
 
A

Alan Silver

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
 
A

Alan Silver

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top