文本框不能为空或输入数字0 c#

时间:2016-06-07 11:25:38

标签: c# .net winforms visual-studio validation

我使用此代码:

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

当我清空文本框时出现我预期的消息框。但是当我输入数字/数字" 0"消息框成功出现?用于验证我使用数字。为验证我只想使用数字1-9。有人可以帮帮我吗?

4 个答案:

答案 0 :(得分:3)

如果要验证数字并且只允许1到9之间的整数,则应使用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;
}

附注:|| textBox5.Text.Length == 0是多余的,因为string.IsNullOrWhiteSpace(textBox5.Text)已经检查过了。

答案 1 :(得分:2)

您的问题是,IsNullOrWhiteSpace仅检查null,而不检查字符'0'。如果您想检查数字,还需要另外检查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;

修改:这是.NET Fiddle

问:

  

如果输入的数字是00000怎么办?

A:

  1. 您可以suggestion使用int.TryParse {/ 3>}

  2. 或者您可以使用以下正则表达式进行验证:\^0*$\

  3. 再次Tim Schmelter

    您需要: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;
    

答案 2 :(得分:2)

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

在这段代码中,您要检查文本长度是否为0

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

如果要检查该框中是否有数字0,则需要检查以下内容:

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

当然,最好的检查方法是尝试解析textBox5.Text并查看是否使用以下代码获得1到9之间的数字:

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

答案 3 :(得分:1)

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