1

I have a textbox, and a submit button. Assuming I do some serverside check, and find the textbox contents are invalid, I want to display an error next to the textbox saying 'invalid text'.

Is there a proper way to do this using validation controls, or do you simply have to stick a label on that you unhide when there's an error?

Edit: Hmm, is there a way to trigger the validation yourself though? I'd rather only do one database query rather than two, if they entered valid data.

3 Answers 3

5

You can use CustomValidator

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="invalid text"></asp:CustomValidator>

You need to handle ServerValidate event

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
    if (Condition == true)
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You could always use a custom validator.

Comments

0

What you can use are Validate controls. You can tie them to a textbox and they perform clientside AND server side validation for you. For simple validations they work like a charm.

My personal experience with more complex validations are that they don't work as nice as you'd expect.

In that case we perform simple validations with the Validate controls (like mandatory fields etc.) en perform complex validations server side and render a label, like you say.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.