如何使用 TextChanged 从字段 B 重新检查字段 A?

发布于 2024-12-19 13:38:36 字数 4417 浏览 1 评论 0原文

我这里遇到一个小问题。这是我的情况:

我输入用户名:tester(有效用户名;避免所有检查),然后输入密码:testerr(有效密码;避免所有检查)。问题是我正在检查相同的输入。在我的代码中,当两个输入相同时,我会看到一条通知。现在,当我输入 tester 作为用户名和密码时,我收到错误,但是当我向密码“testerr”添加附加字符时,我使密码有效,但用户名被检查为无效,说明两者仍然相同,并且使我的验证不可能。

如何避免这种情况呢?我正在考虑重新检查字段 2 中的用户名字段,但我不确定如何进行。

bool passIsValid, userIsValid;

public AuthenticationWindow()
{
    InitializeComponent();

    // Creating TextChanged events which till validate input fields.
    txtUserName.TextChanged += new EventHandler(txtUserName_TextChanged);
    txtPassword.TextChanged += new EventHandler(txtPassword_TextChanged);
}

private void txtUserName_TextChanged(object sender, EventArgs e)
{
    // Checking for empty user name field.
    if (string.IsNullOrEmpty(txtUserName.Text))
    {
        lblMessageUser.Text = "User Name field cannot be empty!";
        lblMessageUser.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
    }
    // Making sure that user name is at least 6 characters long.
    else if (txtUserName.Text.Length < 6)
    {
        lblMessageUser.Text = "User Name field must be at least 6 characters long!";
        lblMessageUser.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
    }
    // Checking for user name made of same repeating character.
    // Invalid example: 'aaaaaa'
    else if (!txtUserName.Text.Distinct().Skip(1).Any())
    {
        lblMessageUser.Text = "User Name cannot be made of repeating the same characters!";
        lblMessageUser.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
    }
    // Making sure that password and user name aren't the same.
    else if (txtUserName.Text == txtPassword.Text)
    {
        lblMessageUser.Text = "User Name and Password can not be the same!";
        lblMessagePass.Text = "User Name and Password can not be the same!";
        lblMessageUser.ForeColor = Color.IndianRed;
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
        passIsValid = false;
    }
    // If all other checks aren't trigered; enable authentication.
    else
    {
        lblMessageUser.Text = "User Name is valid.";
        lblMessageUser.ForeColor = Color.Green;

        userIsValid = true;

        if (passIsValid && userIsValid)
        {
            btnAuthenticate.Enabled = true;
        }
    }
}

private void txtPassword_TextChanged(object sender, EventArgs e)
{
    // Checking for Null or Empty string in password field.
    if (string.IsNullOrEmpty(txtPassword.Text))
    {
        lblMessagePass.Text = "Password field cannot be empty!";
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        passIsValid = false;
    }
    // Making sure that password is at least 6 characters long.
    else if (txtPassword.Text.Length < 6)
    {
        lblMessagePass.Text = "Password field must be at least 6 characters long!";
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        passIsValid = false;
    }
    // Checking for password made of same repeating character.
    // Invalid example: 'aaaaaa'
    else if (!txtPassword.Text.Distinct().Skip(1).Any())
    {
        lblMessagePass.Text = "Password cannot be made of repeating the same characters!";
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        passIsValid = false;
    }
    // Making sure that user name and password are not the same.
    // Security measure.
    else if (txtUserName.Text == txtPassword.Text)
    {
        lblMessageUser.Text = "User Name and Password can not be the same!";
        lblMessagePass.Text = "User Name and Password can not be the same!";
        lblMessageUser.ForeColor = Color.IndianRed;
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
        passIsValid = false;
    }
    // If all other checks aren't trigered; enable authentication.
    else
    {
        lblMessagePass.Text = "Password is valid.";
        lblMessagePass.ForeColor = Color.Green;

        passIsValid = true;

        if (passIsValid && userIsValid)
        {
            btnAuthenticate.Enabled = true;
        }
    }
}

I am having a little problem here. Here is my situation:

I am typing user name: tester (valid user name; avoiding all checks) and then type password: testerr (valid password; avoiding all checks). Problem is that I am checking for the same inputs. And in my code when both inputs are the same I will see a notification. Now, when I type tester as user name and password I get the error, but when I add additional character to my password 'testerr' I am making password valid, but user name is checked as invalid saying that both are still the same and making my validation impossible.

How can avoid this? I was thinking of rechecking user name field from field 2, but I'm not sure how.

bool passIsValid, userIsValid;

public AuthenticationWindow()
{
    InitializeComponent();

    // Creating TextChanged events which till validate input fields.
    txtUserName.TextChanged += new EventHandler(txtUserName_TextChanged);
    txtPassword.TextChanged += new EventHandler(txtPassword_TextChanged);
}

private void txtUserName_TextChanged(object sender, EventArgs e)
{
    // Checking for empty user name field.
    if (string.IsNullOrEmpty(txtUserName.Text))
    {
        lblMessageUser.Text = "User Name field cannot be empty!";
        lblMessageUser.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
    }
    // Making sure that user name is at least 6 characters long.
    else if (txtUserName.Text.Length < 6)
    {
        lblMessageUser.Text = "User Name field must be at least 6 characters long!";
        lblMessageUser.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
    }
    // Checking for user name made of same repeating character.
    // Invalid example: 'aaaaaa'
    else if (!txtUserName.Text.Distinct().Skip(1).Any())
    {
        lblMessageUser.Text = "User Name cannot be made of repeating the same characters!";
        lblMessageUser.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
    }
    // Making sure that password and user name aren't the same.
    else if (txtUserName.Text == txtPassword.Text)
    {
        lblMessageUser.Text = "User Name and Password can not be the same!";
        lblMessagePass.Text = "User Name and Password can not be the same!";
        lblMessageUser.ForeColor = Color.IndianRed;
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
        passIsValid = false;
    }
    // If all other checks aren't trigered; enable authentication.
    else
    {
        lblMessageUser.Text = "User Name is valid.";
        lblMessageUser.ForeColor = Color.Green;

        userIsValid = true;

        if (passIsValid && userIsValid)
        {
            btnAuthenticate.Enabled = true;
        }
    }
}

private void txtPassword_TextChanged(object sender, EventArgs e)
{
    // Checking for Null or Empty string in password field.
    if (string.IsNullOrEmpty(txtPassword.Text))
    {
        lblMessagePass.Text = "Password field cannot be empty!";
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        passIsValid = false;
    }
    // Making sure that password is at least 6 characters long.
    else if (txtPassword.Text.Length < 6)
    {
        lblMessagePass.Text = "Password field must be at least 6 characters long!";
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        passIsValid = false;
    }
    // Checking for password made of same repeating character.
    // Invalid example: 'aaaaaa'
    else if (!txtPassword.Text.Distinct().Skip(1).Any())
    {
        lblMessagePass.Text = "Password cannot be made of repeating the same characters!";
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        passIsValid = false;
    }
    // Making sure that user name and password are not the same.
    // Security measure.
    else if (txtUserName.Text == txtPassword.Text)
    {
        lblMessageUser.Text = "User Name and Password can not be the same!";
        lblMessagePass.Text = "User Name and Password can not be the same!";
        lblMessageUser.ForeColor = Color.IndianRed;
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
        passIsValid = false;
    }
    // If all other checks aren't trigered; enable authentication.
    else
    {
        lblMessagePass.Text = "Password is valid.";
        lblMessagePass.ForeColor = Color.Green;

        passIsValid = true;

        if (passIsValid && userIsValid)
        {
            btnAuthenticate.Enabled = true;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么敢 2024-12-26 13:38:36

您可以将这两个事件合并为一个,然后用户会看到所有错误,并且您的检查可能会成功。

bool passIsValid, userIsValid;

public AuthenticationWindow()
{
    InitializeComponent();

    // Creating TextChanged events which till validate input fields.
    txtUserName.TextChanged += new EventHandler(txtCheck_TextChanged);
    txtPassword.TextChanged += new EventHandler(txtCheck_TextChanged);
}

private void txtCheck_TextChanged(object sender, EventArgs e)
{
    // Checking for empty user name field.
    if (string.IsNullOrEmpty(txtUserName.Text))
    {
        lblMessageUser.Text = "User Name field cannot be empty!";
        lblMessageUser.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
    }
    // Making sure that user name is at least 6 characters long.
    else if (txtUserName.Text.Length < 6)
    {
        lblMessageUser.Text = "User Name field must be at least 6 characters long!";
        lblMessageUser.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
    }
    // Checking for user name made of same repeating character.
    // Invalid example: 'aaaaaa'
    else if (!txtUserName.Text.Distinct().Skip(1).Any())
    {
        lblMessageUser.Text = "User Name cannot be made of repeating the same characters!";
        lblMessageUser.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
    }
    else
    {
        userIsValid = true;
        lblMessageUser.Text = "Password is valid.";
        lblMessageUser.ForeColor = Color.Green;
    }

    // Checking for Null or Empty string in password field.
    if (string.IsNullOrEmpty(txtPassword.Text))
    {
        lblMessagePass.Text = "Password field cannot be empty!";
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        passIsValid = false;
    }
    // Making sure that password is at least 6 characters long.
    else if (txtPassword.Text.Length < 6)
    {
        lblMessagePass.Text = "Password field must be at least 6 characters long!";
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        passIsValid = false;
    }
    // Checking for password made of same repeating character.
    // Invalid example: 'aaaaaa'
    else if (!txtPassword.Text.Distinct().Skip(1).Any())
    {
        lblMessagePass.Text = "Password cannot be made of repeating the same characters!";
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        passIsValid = false;
    }
    else
    {
        passIsValid = true;
        lblMessagePass.Text = "Password is valid.";
        lblMessagePass.ForeColor = Color.Green;
    }

    // Making sure that user name and password are not the same.
    // Security measure.
    if (txtUserName.Text == txtPassword.Text)
    {
        lblMessageUser.Text = "User Name and Password can not be the same!";
        lblMessagePass.Text = "User Name and Password can not be the same!";
        lblMessageUser.ForeColor = Color.IndianRed;
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
        passIsValid = false;
    }

    // If all other checks aren't trigered; enable authentication.
    if (passIsValid && userIsValid)
    {
        btnAuthenticate.Enabled = true;
    }
}

You can merge your both events into one, then the user sees all errors and your check could be successful.

bool passIsValid, userIsValid;

public AuthenticationWindow()
{
    InitializeComponent();

    // Creating TextChanged events which till validate input fields.
    txtUserName.TextChanged += new EventHandler(txtCheck_TextChanged);
    txtPassword.TextChanged += new EventHandler(txtCheck_TextChanged);
}

private void txtCheck_TextChanged(object sender, EventArgs e)
{
    // Checking for empty user name field.
    if (string.IsNullOrEmpty(txtUserName.Text))
    {
        lblMessageUser.Text = "User Name field cannot be empty!";
        lblMessageUser.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
    }
    // Making sure that user name is at least 6 characters long.
    else if (txtUserName.Text.Length < 6)
    {
        lblMessageUser.Text = "User Name field must be at least 6 characters long!";
        lblMessageUser.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
    }
    // Checking for user name made of same repeating character.
    // Invalid example: 'aaaaaa'
    else if (!txtUserName.Text.Distinct().Skip(1).Any())
    {
        lblMessageUser.Text = "User Name cannot be made of repeating the same characters!";
        lblMessageUser.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
    }
    else
    {
        userIsValid = true;
        lblMessageUser.Text = "Password is valid.";
        lblMessageUser.ForeColor = Color.Green;
    }

    // Checking for Null or Empty string in password field.
    if (string.IsNullOrEmpty(txtPassword.Text))
    {
        lblMessagePass.Text = "Password field cannot be empty!";
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        passIsValid = false;
    }
    // Making sure that password is at least 6 characters long.
    else if (txtPassword.Text.Length < 6)
    {
        lblMessagePass.Text = "Password field must be at least 6 characters long!";
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        passIsValid = false;
    }
    // Checking for password made of same repeating character.
    // Invalid example: 'aaaaaa'
    else if (!txtPassword.Text.Distinct().Skip(1).Any())
    {
        lblMessagePass.Text = "Password cannot be made of repeating the same characters!";
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        passIsValid = false;
    }
    else
    {
        passIsValid = true;
        lblMessagePass.Text = "Password is valid.";
        lblMessagePass.ForeColor = Color.Green;
    }

    // Making sure that user name and password are not the same.
    // Security measure.
    if (txtUserName.Text == txtPassword.Text)
    {
        lblMessageUser.Text = "User Name and Password can not be the same!";
        lblMessagePass.Text = "User Name and Password can not be the same!";
        lblMessageUser.ForeColor = Color.IndianRed;
        lblMessagePass.ForeColor = Color.IndianRed;
        btnAuthenticate.Enabled = false;
        userIsValid = false;
        passIsValid = false;
    }

    // If all other checks aren't trigered; enable authentication.
    if (passIsValid && userIsValid)
    {
        btnAuthenticate.Enabled = true;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文