Kaoru
Kaoru

Reputation: 2883

Check if user type a single character or number in textbox c#

I already can make the login for user, so if user type their username or password wrongly in textbox, the message label that says "Invalid username or password" appear. But, i want to when user type a single character or number in textbox when the message label is appear, the message label will not visible to user (visible = false) as user already type a single character or number in textbox. But the message label didn't disappear when user type a single character or number.

This is the code:

private void CheckTextBox(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
            {
                label5.Visible = true;
            }

            else
            {
                label5.Visible = false;
            }
        }

And here is the image:

Below image is when user type wrongly (the username or password), the message label appear:

enter image description here

Below image is when user type a single character or number, but the message label still at there

enter image description here

My question is: How do i set the message label to not show when user type a single character or number in the textbox?

Any help?

Your answer will be great appreciated!

Thank you!

Upvotes: 2

Views: 1022

Answers (5)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Problem : You have not wiredup the CheckTextBox() method for both TextBox1 and TextBox2 TextChanged Event.

Solution : in your Form_Load WireUp the CheckTextBox() method for the Textbox1 and TextBox2 TextChanged Event as below:

  private void Form1_Load(object sender, EventArgs e)
    {
      textBox1.TextChanged += new System.EventHandler(this.CheckTextBox);
      textBox2.TextChanged += new System.EventHandler(this.CheckTextBox);
    }

Suggestion : i think string.IsNullOrWhiteSpace() is more appropriate as it would also check for Whitespace in addition to null and Empty strings.

Try This:

   private void CheckTextBox(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text))
        {
            label5.Visible = true;
        }

        else
        {
            label5.Visible = false;
        }
    }

Upvotes: 2

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38683

Check this code, Should be call this OnTextChanged="CheckTextBox" on your textbox

protected void CheckTextBox(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
            {
                label5.Visible = true;
            }

            else
            {
                label5.Visible = false;
            }
        }

<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="CheckTextBox"></asp:TextBox>

Upvotes: 1

Nagaraj S
Nagaraj S

Reputation: 13484

if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

Try this..use && instead of ||

private void CheckTextBox(object sender, EventArgs e)
{
     if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
     {
         label5.Visible = true;
     }

     else
     {
         label5.Visible = false;
     }
}

Upvotes: 1

Raging Bull
Raging Bull

Reputation: 18747

If I understand you correctly, try this:

private void CheckTextBox(object sender, EventArgs e)
{
     if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
     {
         label5.Visible = true;
     }

     else
     {
         label5.Visible = false;
     }
}

If you change || to && then label5 will be visible only if both textboxes are empty.

Upvotes: 1

Josh Anderson
Josh Anderson

Reputation: 438

This line is checking if either textbox has information in it.

if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

Change the || to && and then the label will only be shown when BOTH textboxes do not have any data.

Upvotes: 1

Related Questions