zohair
zohair

Reputation: 2369

Conditional validation

I have an ASP.NET web app (with C#). In it I have a form with several fields and validators. For one of the validators I only need to validate when:

I know I can't enable/disable the validator on page_load because something might be entered into that textbox. I also tried the following in the onclick event of the submit button but it didn't seem to work:

                    Validator1.Enabled = true;
                    Validator1.Validate();

I also tried Page.Validate() but that didn't work either...

Can anyone please help?

Thanks

Upvotes: 0

Views: 662

Answers (2)

citronas
citronas

Reputation: 19365

Use a customvalidator. Inside the Validation Event you have code like this pseudo code:

OnValidating(object sender, ServerValidateEventArgs e)
{
 if(CertainTextBox.Text.IsNullOrEmpty() && CertainRecordDoesNotExistInDB))
{
 // validate
// and set e.Valid to the desired validation output
}
else
{
 e.IsValid = false;
}
}

Upvotes: 1

Andrey
Andrey

Reputation: 60065

this things should be done by JavaScript on client. Then on submit you should validate at server side.

Upvotes: 0

Related Questions