简体   繁体   中英

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:

在此处输入图片说明

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

在此处输入图片说明

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!

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;
        }
    }

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.

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.

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>
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;
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM