How to validate a control that might be empty

Discussion in 'ASP .Net' started by Alan Silver, Jun 19, 2006.

  1. Alan Silver

    Alan Silver Guest

    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

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Jun 19, 2006
    #1
    1. Advertising

  2. Alan Silver

    Winista Guest

    You willhave to use custom validation.
    Here is an example for dropdownlist.. but you can use the same concept for
    any control or combination of controls on your page.

    http://www.netomatix.com/Development/DropDownCustomValidator.aspx


    "Alan Silver" <> wrote in message
    news:...
    > 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
    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)
    Winista, Jun 19, 2006
    #2
    1. Advertising

  3. It sounds like a job for the CustomValidator control.

    This will get you started:

    http://msdn2.microsoft.com/en-us/li...customvalidator.clientvalidationfunction.aspx

    --
    -Demetri


    "Alan Silver" wrote:

    > 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
    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)
    >
    =?Utf-8?B?RGVtZXRyaQ==?=, Jun 19, 2006
    #3
  4. Alan Silver

    Alan Silver Guest

    In article <>, Alan Silver
    <> writes
    <snip>
    >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

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Jun 19, 2006
    #4
  5. Alan Silver

    Alan Silver Guest

    In article <>, Demetri
    <> writes
    >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.

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Jun 19, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ned
    Replies:
    2
    Views:
    487
    Jerry
    Feb 9, 2004
  2. js
    Replies:
    2
    Views:
    2,251
  3. edkawas
    Replies:
    0
    Views:
    491
    edkawas
    Jun 24, 2005
  4. ButlerDJIAM
    Replies:
    0
    Views:
    553
    ButlerDJIAM
    Nov 9, 2006
  5. fniles
    Replies:
    4
    Views:
    825
    fniles
    Nov 29, 2005
Loading...

Share This Page