I want to perform password and confirm password validations using material components only,and an error message below the confirm password field if confirm password field doesn't match
And if it is empty
.Tried many resources unable to achieve.
Tried this video too.
This is the material component i am looking for
HTML
<mat-form-field >
<input matInput placeholder="New password" [type]="hide ? 'password'
: 'text'" [formControl]="passFormControl" required>
<mat-icon matSuffix (click)="hide = !hide">{{hide ? 'visibility' :
'visibility_off'}}</mat-icon>
<mat-error *ngIf="passFormControl.hasError('required')">
Please enter your newpassword
</mat-error>
</mat-form-field>
<mat-form-field >
<input matInput placeholder="Confirm password" [type]="hide ?
'password' : 'text'" [formControl]="confirmFormControl"
required>
<mat-icon matSuffix (click)="hide = !hide">{{hide ? 'visibility' :
'visibility_off'}}</mat-icon>
<mat-error *ngIf="confirmFormControl.hasError('required')">
Confirm your password
</mat-error>
</mat-form-field>
TS
import {Component, OnInit } from '@angular/core';
import {FormControl, FormGroupDirective, NgForm, Validators} from
'@angular/forms';
import {ErrorStateMatcher} from '@angular/material/core';
@Component({
selector: 'asd-set-pass',
templateUrl: './set-pass.component.html',
styleUrls: ['./set-pass.component.css']
})
passFormControl = new FormControl('', [
Validators.required,
]);
confirmFormControl = new FormControl('', [
Validators.required,
]);
hide =true;
}
It's validating the following conditions fine 1)If password and confirm password fields are empty its showing error text.
I want to compare to fields in (.ts) file like how its validating for empty field, and an error to come if confirm password field is empty.
In case you have more than just Password and Verify Password fields. Like this the Confirm password field will only highlights error when user write something on this field:
validators.ts
register.component.ts
register.component.html
i hope it helps!
I am using angular 6 and I have been searching on best way to match password and confirm password. This can also be used to match any two inputs in a form. I used Angular Directives. I have been wanting to use them
Now in your component
I hope this one helps
My answer is very simple>i have created password and confirm password validation using template driiven in angular 6
My html file
My typescript file
when clicking on submit button i check whether value of i is true or false
if true
I found a bug in AJT_82's answer. Since I do not have enough reputation to comment under AJT_82's answer, I have to post the bug and solution in this answer.
Here is the bug:
Solution: In the following code:
Change
control.parent.invalid
tocontrol.parent.hasError('notSame')
will solve this problem.After the small changes, the problem solved.
The simplest way imo:
(It can also be used with emails for example)
In your Component:
I did it like this. hope this will help you.
HTML :
TS File :