目录

设置密码时,可输入的长度与提示长度不符

问题现象

设置密码时提示6~20位,但是实际在输入11位后就无法继续输入了。

背景知识

问题定位

  1. 定位到登录注册部分,查到输入密码的TextInput。
  2. 查看对密码长度的校验规则,找到maxLength关键词是否为提示的最大值。

分析结论

maxLength设置的最大值只有11,导致密码最大值与提示的长度不同。

修改建议

通过设置maxLength来限制密码长度,并在onChange事件中判断键入的密码长度,小于6位提示用户密码长度过短。可参考以下代码:

@Entry
@Component
struct TextInputDemo {
  @State password: string = '';
  @State state: boolean = false;

  build() {
    Column({ space: 20 }) {
      Text(`当前输入密码长度:${this.password.length}`);

      TextInput({ text: this.password })
        .width('80%')
        .type(InputType.Password)
        .showPasswordIcon(false)
        .inputFilter('[0-9A-Za-z]')
        .maxLength(20)
        .onChange((v) => {
          this.password = v;
          console.info(`输入字符为:${this.password},长度:${this.password.length}`);
          if (this.password.length >= 6) {
            this.state = true;
          } else {
            this.state = false;
          }
        });

      if (!this.state) {
        Text('请输入6-20位密码,包含大小写字母和数字')
          .fontColor(Color.Red);
      }
    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .height('100%');
  }
}