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:
- a certain textbox is empty
- and a certain record does not exist in the database (this is already handled).
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
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;
}
}
this things should be done by JavaScript on client. Then on submit you should validate at server side.
精彩评论