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.
Just do a standard custom validator and verify first if the form itself is defined, otherwise it will throw an error that says the form is undefined, because at first it will try to run the validator before the form is constructed.
It just checks that the new password field has the same value that the confirm password field. Is a validator specific for the confirm password field instead of the whole form.
You just have to verify that
this.changePasswordForm
is defined because otherwise it will throw an undefined error when the form is built.It works just fine, without creating directives or error state matchers.
This question could be solved with a combination of these two answers: https://stackoverflow.com/a/43493648/6294072 and https://stackoverflow.com/a/47670892/6294072
So first of all, you would need a custom validator for checking the passwords, that could look like this:
and you would create a formgroup for your fields, instead of just two form controls, then mark that custom validator for your form group:
and then as mentioned in other answer, the
mat-error
only shows if a FormControl is invalid, so you need an error state matcher:in the above you can tweak when to show error message. I would only show message when the
password
field is touched. Also I would like above, remove therequired
validator from theconfirmPassword
field, since the form is not valid anyway if passwords do not match.Then in component, create a new
ErrorStateMatcher
:Finally, the template would look like this:
Here's a demo for you with the above code: StackBlitz
*This solution is for reactive-form
You may have heard the confirm password is known as cross-field validation. While the field level validator that we usually write can only be applied to a single field. For cross-filed validation, you probably have to write some parent level validator. For specifically the case of confirming password, I would rather do:
And here is the template:
Reactive Forms in Angular lets us define custom validators very easily. In this tutorial, I am going to create confirm password validation. To do that I will be creating a separate folder by the name of must-match and keep my custom validator file there. I will name it validate-password angular .ts so it will sound generic. must-match > validate-password.ts
you can see at here: Confirm password Validation angular
You can simply use password field value as a pattern for confirm password field. For Example :
It's not necessary to use nested form groups and a custom ErrorStateMatcher for confirm password validation. These steps were added to facilitate coordination between the password fields, but you can do that without all the overhead.
Here is an example:
Note that we are passing additional context to the validatePasswords method (whether the source is password1 or password2).
Note here that when the passwords match, we coordinate with the other password field to have its validation updated. This will clear any stale password mismatch errors.
And for completeness sake, here are the getters that define
this.password1
andthis.password2
.