首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 11物料表单字段一行显示验证错误

Angular 11物料表单字段一行显示验证错误
EN

Stack Overflow用户
提问于 2021-06-17 12:16:08
回答 2查看 54关注 0票数 0

对于至少有一个no、char、special char和number的正则表达式赋值,我一直在与不显示的错误作斗争。

TypeScirpt

代码语言:javascript
复制
<mat-form-field class="form-element">
   <input matInput id="password1" placeholder="New password" formControlName="password1">            
   <label   *ngIf="resetPasswordForm.get('password1').errors && resetPasswordForm.get('password1').hasError('required') && 
        (resetPasswordForm.get('password1').dirty || resetPasswordForm.get('password1').touched)" >Errorasdf</label>
</mat-form-field>  

resetPasswordForm = this.formBuilder.group({
  password1: ['',[ Validators.required, Validators.pattern(/^(?=\D*\d)(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=.*[$@$!%*?&*()]).{8,16}$/) ]],
  password2: ['',[ Validators.required, Validators.pattern(/^(?=\D*\d)(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=.*[$@$!%*?&*()]).{8,16}$/) ]],
  
})
EN

回答 2

Stack Overflow用户

发布于 2021-06-17 12:16:08

找到了一个有效的线条,看到了一些可怕的冗长的解决方案,这个效果很好。

代码语言:javascript
复制
      <mat-form-field class="form-element">
        <input matInput id="password1" placeholder="New password" formControlName="password1">
        <mat-error style="color: red;" *ngIf="resetPasswordForm.controls['password1']">Minumum 8 length with 1 upper, lower case letters, 1 symbol and 1 number</mat-error>
      </mat-form-field> 
票数 0
EN

Stack Overflow用户

发布于 2021-06-18 04:52:25

HTML和Typescript之前的解决方案似乎只在第一次输入值时有效。如果发生错误,Validator事件将不会清除错误消息。这里是一个密码测试和工作代码,A-Z,a-z,0-9,特殊字符和长度为8个字符,这必须是任何顺序。

代码语言:javascript
复制
    regexp: RegExp = /(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])/;

resetPasswordForm = this.formBuilder.group({
  password1: ['',[ Validators.minLength(8), Validators.pattern(this.regexp)]],
  password2: ['',[ Validators.minLength(8), Validators.pattern(this.regexp)]],
  
})

      <mat-form-field class="form-element">
        <input matInput id="password1" placeholder="New password" formControlName="password1">
        <mat-error style="color: red;" *ngIf="resetPasswordForm.controls['password1']">Minumum 8 length with 1 upper, lower case letters, 1 symbol and 1 number</mat-error>
      </mat-form-field>  

      
      <BR>
      <mat-form-field class="form-element">
        <input matInput id="password2" placeholder="Confirm password" formControlName="password2">
        <mat-error style="color: red;" *ngIf="resetPasswordForm.controls['password2']">Minumum 8 length with 1 upper, lower case letters, 1 symbol and 1 number</mat-error>
      </mat-form-field> 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68012847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档