WinForms:如何在c#的文本框中检查最小字符数?

时间:2021-05-28 20:54:09

I am trying to make sure that user uses at least 6 characters as a password in my program.

我正在尝试确保用户在我的程序中使用至少6个字符作为密码。

I know how to set maximum size by using MaxLength, but how I do this for the minimum length?

我知道如何使用MaxLength设置最大大小,但是我如何做到最小长度呢?

4 个解决方案

#1


1  

Although competent_tech gives you my recommended approach, a primitive solution would be the following:

尽管competent_tech给出了我推荐的方法,但一个基本解决方案应该是这样的:

Drop a label on your form and give it an ErrorText.

在你的表格上写下一个标签,然后给它一个错误的文字。

Use the following code for your textbox KeyDown event:

为你的文本框下键事件使用以下代码:

protected override void OnLoad(object sender, EventArgs e)
{
    base.OnLoad(sender, e);

    txtPassword.KeyDown += OnPasswordKeydown;     
}

protected void OnPasswordKeydown(object sender, KeyEventArgs e)
{
    bool isValid = txtPassword.Text.Length < 6;

    ErrorText.Visible = isValid;
    AcceptButton.Visible = isValid;
}

#2


3  

if (passwordTextBox.Text.Length < 6)
{
    MessageBox.Show("Passwords must be at least 6 characters long.");
    return /*false*/;
}

// Do some stuff...

return /*true*/;

#3


2  

If you are going to have multiple user interfaces where the user can enter their password (web, mobile, windows client, etc) or would provide a service for doing the same (web, wcf, etc), then I think your best option is to catch this type of error at the most common level to all of these platforms.

如果你要有多个用户界面,用户可以输入密码(网络、移动、windows客户端等)或将提供一个服务做同样的(web、wcf等等),那么我认为你最好的选择是捕捉这种类型的错误在最常见的水平所有这些平台。

We generally implement business rules like this in the database (through stored procedures) so that we have one well-known location to check and change these rules.

我们通常在数据库中(通过存储过程)实现这样的业务规则,以便有一个众所周知的位置来检查和更改这些规则。

If you are using a database that doesn't support stored procedures, then you could implement this functionality in the "business layer", or the set of code responsible for performing the business logic for your application.

如果您正在使用一个不支持存储过程的数据库,那么您可以在“业务层”或负责为应用程序执行业务逻辑的一组代码中实现这个功能。

#4


1  

Use the validating method on your password textbox to enforce length.

使用密码文本框上的验证方法来执行长度。

        if (TextBox1.Text.Length < 6)
        {
            MessageBox.Show("password too short");
            TextBox1.Focus();
        }

#1


1  

Although competent_tech gives you my recommended approach, a primitive solution would be the following:

尽管competent_tech给出了我推荐的方法,但一个基本解决方案应该是这样的:

Drop a label on your form and give it an ErrorText.

在你的表格上写下一个标签,然后给它一个错误的文字。

Use the following code for your textbox KeyDown event:

为你的文本框下键事件使用以下代码:

protected override void OnLoad(object sender, EventArgs e)
{
    base.OnLoad(sender, e);

    txtPassword.KeyDown += OnPasswordKeydown;     
}

protected void OnPasswordKeydown(object sender, KeyEventArgs e)
{
    bool isValid = txtPassword.Text.Length < 6;

    ErrorText.Visible = isValid;
    AcceptButton.Visible = isValid;
}

#2


3  

if (passwordTextBox.Text.Length < 6)
{
    MessageBox.Show("Passwords must be at least 6 characters long.");
    return /*false*/;
}

// Do some stuff...

return /*true*/;

#3


2  

If you are going to have multiple user interfaces where the user can enter their password (web, mobile, windows client, etc) or would provide a service for doing the same (web, wcf, etc), then I think your best option is to catch this type of error at the most common level to all of these platforms.

如果你要有多个用户界面,用户可以输入密码(网络、移动、windows客户端等)或将提供一个服务做同样的(web、wcf等等),那么我认为你最好的选择是捕捉这种类型的错误在最常见的水平所有这些平台。

We generally implement business rules like this in the database (through stored procedures) so that we have one well-known location to check and change these rules.

我们通常在数据库中(通过存储过程)实现这样的业务规则,以便有一个众所周知的位置来检查和更改这些规则。

If you are using a database that doesn't support stored procedures, then you could implement this functionality in the "business layer", or the set of code responsible for performing the business logic for your application.

如果您正在使用一个不支持存储过程的数据库,那么您可以在“业务层”或负责为应用程序执行业务逻辑的一组代码中实现这个功能。

#4


1  

Use the validating method on your password textbox to enforce length.

使用密码文本框上的验证方法来执行长度。

        if (TextBox1.Text.Length < 6)
        {
            MessageBox.Show("password too short");
            TextBox1.Focus();
        }