通过设置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%'); } }