繁体   English   中英

C#textBox验证

[英]C# textBox validation

当我刚开始使用C#时,开始时会遇到一些麻烦。 我正在尝试制作一个简单的登录系统,这是到目前为止我所获得的:

private void button1_Click(object sender, EventArgs e)
{
    string username = "Admin";
    string password = "root";

    if (!string.IsNullOrEmpty(textBox1.Text) && (!string.IsNullOrEmpty(textBox2.Text)))
        MessageBox.Show("Fields are not filled");

    if (!string.IsNullOrEmpty(textBox1.Text))
        MessageBox.Show("No Password inserted");

    if (!string.IsNullOrEmpty(textBox2.Text))
        MessageBox.Show("No username inserted");

    if ((textBox1.Text == username) && (textBox2.Text == password))
        MessageBox.Show("Login Succeed");
    else
        MessageBox.Show("Login failed");
}

当我输入Admin作为用户名和root作为密码时,它使我登录成功;当我随机输入Username和Password时,它使我登录失败,该部分可以正常工作。 更多有关“未插入”部分以及何时未填写字段的信息。

它只是不执行任何操作:')一直说“登录失败”,但有解决方法吗?

更换

!string.IsNullOrEmpty()

string.IsNullOrWhiteSpace()

所以完整的解决方案:

private void button1_Click(object sender, EventArgs e)
{
    string username = "Admin";
    string password = "root";

    if (string.IsNullOrWhiteSpace (textBox1.Text))
        MessageBox.Show("No Password inserted");
    else if (string.IsNullOrWhiteSpace (textBox2.Text))
        MessageBox.Show("No username inserted");
    else if ((textBox1.Text == username) && (textBox2.Text == password))
        MessageBox.Show("Login Succeed");
    else
        MessageBox.Show("Login failed");

}

请尝试以下操作:

private void button1_Click(object sender, EventArgs e)
        {
            string username = "Admin";
            string password = "root";

            //When both are empty
            if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("Fields are not filled");
                return;
            }

            //When username is empty    
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("No username inserted");
                return;
            }

            //When password is empty
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("No password inserted");
                return;
            }

            if ((textBox1.Text == username) && (textBox2.Text == password))
                MessageBox.Show("Login Succeed");
            else
                MessageBox.Show("Login failed");

        }

主要问题是您基本要问的第一条IF语句中的逻辑:

IF(Not-IsNullOrEmpty(LOGIN) AND Not-IsNullOrEmpty(PASS))
    SHOW MESSAGE

这就是为什么它不起作用的原因。

我可能会重写一下它,以免重复我自己,并使它更具可读性:

private void button1_Click(object sender, EventArgs e)
{
    string username = "Admin";
    string password = "root";
    var message = string.Empty;
    bool loginEmpty = string.IsNullOrEmpty(textBox1.Text),
         passEmpty = string.IsNullOrEmpty(textBox2.Text),
         loggedIn = (textBox1.Text == username) && (textBox2.Text == password);

    if (!loggedIn) 
    {
        if (loginEmpty && passEmpty)
            message = " - Fields are not filled";
        else if (passEmpty)
            message = " - No Password inserted";
        else if (loginEmpty)
            message = " - No username inserted";

        MessageBox.Show("Login failed" + message);
        return;
    }

    MessageBox.Show("Login succeded");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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