Underdog
Underdog

Reputation: 129

Textbox cannot empty or enter digit 0 c#

i use this code :

 private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == 13)
        {
            if (string.IsNullOrWhiteSpace(textBox5.Text) || textBox5.Text.Length == 0) 
            {
                MessageBox.Show("Textbox Cannot Empty or digit 0");
                textBox5.Focus();
            }
            else
            {
                MessageBox.Show("Success!");
            }
            e.Handled = true;
        }
    }

when I empty the textbox appears messagebox I expected . but when I enter the number/digit "0" appears messagebox success ? for validation i used numeric. for validation I only want to use the numbers 1-9. anybody can help me?

Upvotes: 2

Views: 427

Answers (4)

Maximilian Ast
Maximilian Ast

Reputation: 3499

Your problem is, that IsNullOrWhiteSpace checks only for null, not for the char '0'. If you want to check the digit too, you need to check in addition for textBox5.Text.Equals("0"):

if (string.IsNullOrWhiteSpace(textBox5.Text) || textBox5.Text.Equals("0")) 
{
    MessageBox.Show("Textbox Cannot Empty or digit 0");
    textBox5.Focus();
}
else
{
    MessageBox.Show("Success!");
}
e.Handled = true;

Edit: Here's a .NET Fiddle


Q:

what if the digits entered are 00000 ??

A:

  1. You can use int.TryParse as in the suggestion of Tim Schmelter

  2. Or you can use the following Regex for vaidation: \^0*$\

Again the .NET Fiddle

Here you need: using System.Text.RegularExpressions;

if (string.IsNullOrWhiteSpace(textBox5.Text) || Regex.Match(textBox5.Text, "^0*$").Success) 
{
    MessageBox.Show("Textbox Cannot Empty or digit 0");
    textBox5.Focus();
}
else
{
    MessageBox.Show("Success!");
}
e.Handled = true;

Upvotes: 2

Razvan Dumitru
Razvan Dumitru

Reputation: 12462

if (string.IsNullOrWhiteSpace(textBox5.Text) || textBox5.Text.Length == 0) 
            {
                MessageBox.Show("Textbox Cannot Empty or digit 0");
                textBox5.Focus();
            }
            else
            {
                MessageBox.Show("Success!");
            }

In this piece of code you're checking if the text length is 0

// "" will have length 0 
// "0" will have length 1 

If you want to check if you have the digit 0 in that box you need to check the following:

if (string.IsNullOrWhiteSpace(textBox5.Text) || 
    textBox5.Text == "0")

textBox5.Text.Length == 0 // you don't need this anymore if youre using IsNullOrWhiteSpace as IsNullOrWhiteSpace checks for null, string.Empty, white spaces

Of course the most beautiful way to check that is to try to parse textBox5.Text and to see if you get a digit between 1 and 9 using the following code:

int.TryParse(textBox5.Text, out number) && number > 0 && number < 10 

Upvotes: 2

Alper Şaldırak
Alper Şaldırak

Reputation: 1054

private void textBox5_TextChanged(object sender, EventArgs e)
        {
            string[] removeCaracter = { "0",... };
            foreach (var item in removeCaracter)
            {
                textBox5.Text = textBox5.Text.Replace(item, "");
                textBox5.SelectionStart = textBox5.Text.Length ;
                textBox5.SelectionLength = 0;
            }
        }

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460228

If you want to validate numbers and allow only integers between 1 and 9, you should use int.TryParse:

if (e.KeyChar == 13)
{
    int number;
    if(int.TryParse(textBox5.Text, out number) && number >= 1 && number <= 9)
    {
        MessageBox.Show("Success!");
    }
    else
    {
        MessageBox.Show("Textbox must contain an integer between 1 and 9");
        textBox5.Focus();
    }
    e.Handled = true;
}

Side-note: || textBox5.Text.Length == 0 is redundant since string.IsNullOrWhiteSpace(textBox5.Text) checks that already.

Upvotes: 3

Related Questions