0

I am using C#.net.

I have two textboxes (textbox1 / textbox2). If both are empty it needs to display a error message.

I tried a CustomValidator control and had it validating on textbox2. Within my code behind I checked whether both were empty if so it returned false (args = false). However when I built the application, it didn’t even access the button event. Can I not use this?

Here is what I have tried:

  <asp:CustomValidator ID="customValidator" runat="server" 
    ErrorMessage="You must provide either a phone number or email address." 
    ControlToValidate="textbox2" 
    OnServerValidate="PhoneEmailCustomValidator_ServerValidate" />

protected void PhoneEmailCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (string.IsNullOrEmpty(texbox1.Text) && string.IsNullOrEmpty(textbox2.Text))
            {
                Debug.Write("Within if statement");

                args.IsValid = false;
            }
        }

Thanks in advance for any help.

Clare

1
  • Added the code that I have tried Commented Oct 9, 2009 at 8:27

1 Answer 1

5

Set the CustomValidator's ValidateEmptyText property to true. Otherwise validation won't be triggered if the validated control is empty.

Sign up to request clarification or add additional context in comments.

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.