我希望你能给我很多帮助.我无法验证密码字段,也无法使用版本12中的Angular material 确认密码.我在html中使用下面的 struct .

<div>
  <mat-form-field>
    <mat-label>Password</mat-label>
    <input matInput 
           type="password" 
           placeholder="Digite sua senha" 
           [type]="hide ? 'password' : 'text'"
           [formControl]="passwordFormControl" />
    <mat-icon matSuffix (click)="hide = !hide">
           {{ hide ? 'visibility' : 'visibility_off' }}
    </mat-icon>
    <mat-error>               
    </mat-error>
  </mat-form-field>
</div>
<div>
  <mat-form-field>
    <mat-label>Confirm Password</mat-label>
    <input matInput
           type="password" 
           placeholder="Digite novamente sua senha"
           [type]="hide ? 'password' : 'text'" 
           [formControl]="passwordFormControl"/>
    <mat-icon matSuffix (click)="hide = !hide">
      {{ hide ? 'visibility' : 'visibility_off' }}
    </mat-icon>
    <mat-error>               
    </mat-error>
  </mat-form-field>
</div>

推荐答案

自定义验证:密码确认

Mustafa是对的,当你使用角material 和mat-error个标签时,最好使用react 形式.但是,这里有一个更完整的版本,包括所有验证消息,因为您需要'A LOT OF'帮助.

我也改进了Mustafa的验证器.这将帮助您消除验证消息的所有奇怪之处.

可见性图标逻辑已删除,因为material 密码输入已内置此功能.

脚本:

form: FormGroup;

constructor(private fb: FormBuilder) {}

ngOnInit() {
  this.form = this.fb.group(
    {
      password: [
        '',
        [
          Validators.required,
          Validators.minLength(8),
          Validators.maxLength(100),
        ],
      ],
      confirmPassword: [
        '',
        [
          Validators.required,
          Validators.minLength(8),
          Validators.maxLength(100),
        ],
      ],
    },
    { validator: CustomValidators.MatchingPasswords }
  );
}

改进的自定义验证器:

export class CustomValidators {

    static MatchingPasswords(control: AbstractControl) {
        const password = control.get('password').value;
        const confirmPassword = control.get('confirmPassword').value;
        const currentErrors = control.get('confirmPassword').errors
        const confirmControl = control.get('confirmPassword')

        if (compare(password, confirmPassword)) {
            confirmControl.setErrors({...currentErrors, not_matching: true});
        } else {
            confirmControl.setErrors(currentErrors)
        }
    }
}

function compare(password: string,confirmPassword: string) {
    return password !== confirmPassword && confirmPassword !== ''
}

模板:

<form [formGroup]="form">
  <mat-form-field>
    <mat-label>Password</mat-label>
    <input
      matInput
      type="password"
      placeholder="Password"
      formControlName="password"
    />
    <mat-error *ngIf="form.get('password').hasError('required')">
      Password is required!
    </mat-error>
    <mat-error *ngIf="form.get('password').hasError('minlength')">
      Password should be longer than 8 characters!
    </mat-error>
    <mat-error *ngIf="form.get('password').hasError('maxlength')">
      Password can't be longer than 100 characters!
    </mat-error>
  </mat-form-field>

  <mat-form-field>
    <mat-label>Confirm Password</mat-label>
    <input
      matInput
      placeholder="Confirm your password..."
      formControlName="confirmPassword"
      type="password"
    />
    <mat-error *ngIf="form.get('confirmPassword').hasError('required')">
      Password confirmation is required!
    </mat-error>
    <mat-error *ngIf="form.get('confirmPassword').hasError('minlength')">
      Password should be longer than 8 characters!
    </mat-error>
    <mat-error *ngIf="form.get('confirmPassword').hasError('maxlength')">
      Password can't be longer than 100 characters!
    </mat-error>
    <mat-error *ngIf="form.get('confirmPassword').hasError('not_matching')">
      The password doesn't match!
    </mat-error>
  </mat-form-field>
</form>

You can clean up all the 100 tags, by providing the message with a function.

这里有一个关于Stackblitz的例子供您使用.

Angular相关问答推荐

Angular - MSAL用户缺少角色

升级到angular 17并转换为独立的加载器拦截器后,

FormArray包含嵌套的FormGroups.如何访问嵌套的表单组控件?

tabindex on button on button in MatMenu in angular不工作

如何使用ChangeDetectionStrategy.OnPush?

如何通过innerHtml在模板中显示动态插入的动感图标?

ANGLE v16独立组件不能与拦截器一起工作?

区分material 手风琴

如何在 Angular 库中将依赖项声明为可选?

Angular6:RxJS switchMap 运算符未按预期工作

Angular 13 - 使用 PUT 时出现 400 错误(错误请求)

Angular UNIVERSAL prerender 错误方法 Promise.prototype.then 调用不兼容的接收器 [object Object]

Angular 2模板驱动表单访问组件中的ngForm

react形式的formGroup.get与formGroup.controls

ViewChild 和 ContentChild 的所有有效 Select 器是什么?

在 Angular 4 中播放声音

Angular2 中的providers提供者是什么意思?

如何设置背景 colored颜色 IONIC 4

Angular 4 Bootstrap 下拉菜单需要 Popper.js

BrowserAnimationsModule 和 NoopAnimationsModule 有什么区别?