1

I am trying to implement validations on textbox like if null then display a message to fill the empty field and to check the length of the text entered. I have written the code for it inside TextBox_TextChanged event but it's not working. The event is not getting fired and the user can is able to signup without a username, that is my problem at the moment. Do I have to trigger the event manually? Here is a glimpse of what I am doing:

protected void FirstN_TextBox_TextChanged(object sender, EventArgs e)
    {
        String firstNameEntered = FirstN_TextBox.Text;
        if (firstNameEntered != null)
        {
            if (firstNameEntered.Length <= 20)
            {
                MessageBox.Show("Inside text box");
            }
            else
            {

            }

        }
        else
        {
            FirstN_TextBox.Focus();
            MessageBox.Show("Please fill the marked field");
        }
13
  • show your html code Commented Apr 27, 2017 at 3:22
  • Are you using ASP.NET WebForm? How are you using on the client side (.asp or .aspx files)? I will suggest using JavaScript instead of C# code behind. Commented Apr 27, 2017 at 3:22
  • if you are using asp.net this MessageBox.Show can't be done. Commented Apr 27, 2017 at 3:24
  • without username???? use requirefieldvalidators Commented Apr 27, 2017 at 3:39
  • @reds I havn't changed anything inside the HTML code but still if you want to see: <asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px" OnTextChanged="FirstN_TextBox_TextChanged"></asp:TextBox> Commented Apr 27, 2017 at 3:41

1 Answer 1

2

change it like this:

From:

<asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px"   OnTextChanged="FirstN_TextBox_TextChanged"></asp:TextBox>

To:

 <asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px"   OnTextChanged="FirstN_TextBox_TextChanged" AutoPostBack ="true"></asp:TextBox>
Sign up to request clarification or add additional context in comments.

6 Comments

Clean and build your project after you change it.
I have done the same but still validation not working.
Its tested before i post. don't know what you did. try to check spaces and remove
I have done exactly the same but didn't work. So I switched to validate controls. But I appreciate your time and efforts. Thanks.... :)
do you want to try keypress event?
|

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.