I Just need to compare password and validate confirm password fields. When Password do not match, I need to Display message and show Error : Passwords do not match And When Error is displayed I need set invalid of the input field and thus disable button.
<form name="SignUpForm" #SignUpForm="ngForm"><br>
<label><i class="fa fa-key"></i>
<input
type="password"
name="Pwd"
placeholder="Password"
required
pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,}"
[(ngModel)]="SignUpNgsForm.Pwd"
#Pwd="ngModel">
</label>
<div *ngIf="(Pwd.touched || Pwd.dirty) && Pwd.invalid" class="ErrorCls">
<span *ngIf="Pwd.errors.required">Password is required.</span>
<span *ngIf="Pwd.errors.pattern">Enter Strong password.</span><br>
</div>
<label><i class="fa fa-key"></i>
<input
type="password"
name="RptPwd"
placeholder="Repeat-Password"
required
pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,}"
[(ngModel)]="SignUpNgsForm.RptPwd"
#RptPwd="ngModel">
</label>
<div *ngIf="(RptPwd.touched || RptPwd.dirty) && RptPwd.invalid" class="ErrorCls">
<span *ngIf="RptPwd.errors.required && RptPwd.invalid">Repeat-Password is required.</span>
<span *ngIf="RptPwd.errors.pattern">Enter Strong password.</span>
</div>
<div *ngIf="(RptPwd.touched || RptPwd.dirty)" class="ErrorCls">
<span *ngIf="!RptPwd.errors && SignUpNgsForm.Pwd != SignUpNgsForm.RptPwd">Passwords do not match</span>
</div>
<br>
<button [disabled]="!SignUpForm.form.valid" (click)="SignUp(SignUpForm)">Sign Up</button>
</form>
Also Tried :
Add in Component.html
<div *ngIf="(RptPwd.touched || RptPwd.dirty)" class="ErrCls">
<span *ngIf="CheckMatchPassword(RptPwd)">Passwords do not match</span>
</div>
Add in Component.ts
CheckMatchPassword(RptPwd)
{
if(!RptPwd.errors && this.SignUpNgsForm.Pwd != this.SignUpNgsForm.RptPwdreturn)
{
return RptPwd.control.setErrors({'mismatch': true});
}
else
{
return RptPwd.control.setErrors(null);
}
}
For this I get error :
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: false'. Current value: 'ngIf: true'.
How do I get this, without using FormControl or Reactive Forms.