簡體   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