views:

236

answers:

2

Hi all,

Right now i have an asp.net 2.0 app which allows a user to search by the following fields

Location (Required if there is nothing in idnumber field)
Address  (Required if there is nothing in idnumber field)
Zip      (Required if there is nothing in idnumber field)

**OR**

IDNumber. (Required if there is nothing in any of the other fields)

What i'd like to be able to do is validate this client side on button click and display a summary of errors.

i.e. if a user leaves every criteria blank. I'd like to display "You must enter a IDNumber or "Location, Address, and Zip to continue"

I've never used the Custom Validation control so here are some questions. 1) Is it able to do this? 2) Does anyone have an example of how to do this?

Thanks

A: 

It is fairly simple to use a custom validator. Add one to your page, and choose the ServerValidate event, which will generate a function like this (example in C#):

protected void CustomValidator1_ServerValidate(object source, 
                                                ServerValidateEventArgs args)
{
    // Your validation logic goes here... set args.IsValid = true if it passes, 
    // false otherwise. Here is an example...

    args.IsValid = false;
    if(txtIDNumber.Text.Length > 0)
    {
         args.IsValid = true;
    }
    else if (txtLocation.Text.Length > 0
             && txtAddress.Text.Length > 0
             && txtZip.Text.Length > 0)
    {
         args.IsValid = true;
    }
}
John Rasch
+1  A: 

You can use the ClientValidationFunction property of a CustomValidator control to specify a Javascript function that will validate your form. You'll need to write the JavaScript for the validation. Unless you're writing an application where you can be absolutely sure that all of your clients have JavaScript enabled, I highly recommend you also use the OnServerValidate property to also provide server-side validation.

Jeremy Frey