Я проверил пароль и подтвердил пароль на основе типа подтверждения пароля ...
HTML file ...
<div class="form-group">
<input type="password" formControlName="password" class="form-control input-underline input-lg" id="password" placeholder="Password">
</div>
<div class="form-group">
<input type="password" formControlName="confirmPassword" class="form-control input-underline input-lg" id="confirmPassword" (input)="checkPassword()" placeholder="Repeat Password">
</div>
<div *ngIf="passwordNotMatched">Password and confirmPassword is not matched</div>
In .ts file ...
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
constructor(private formBuilder: FormBuilder) {}
ngOnInit() {
this.registerForm = this.formBuilder.group({
name: ['', [Validators.required]],
email: ['', [Validators.required]],
password: ['', [Validators.required, Validators.minLength(3)]],
confirmPassword: ['', [Validators.required, Validators.minLength(3)]]
});
}
get f() { return this.registerForm.controls; }
checkPassword(){
console.log("Password : ",this.registerForm.value.password);
console.log("Conform : " , this.registerForm.value.confirmPassword);
if(this.registerForm.value.password == this.registerForm.value.confirmPassword){
this.passwordNotMatched = false;
}else{
this.passwordNotMatched = true;
}
}
Пока пароль не совпадает ...
![Password not matched](https://i.stack.imgur.com/ykk1i.png)
Когда пароль совпадает ...
![enter image description here](https://i.stack.imgur.com/qYGt0.png)
Для установки в <mat-error>
:
<div class="example-container">
<mat-form-field>
<input type="password" formControlName="confirmPassword" class="form-control input-underline input-lg" id="confirmPassword" (input)="checkPassword()" placeholder="Repeat Password">
<mat-error *ngIf="passwordNotMatched">Password and confirmPassword is not matched</mat-error>
</mat-form-field>
</div>
Примечание. Приведенный выше код <mat-error>
приведен только для справки. Это непроверенный код из-за неинсталляции материала в моем проекте.