visit
In this short article I will show you how to use an npm package that does exactly what you need in 3 easy steps without any effort.
npm i ng-validate-equal
import { ValidateEqualModule } from 'ng-validate-equal';
@NgModule({
declarations: [],
imports: [
ValidateEqualModule
],
providers: [],
})
<form> </form>
tag
ngValidateEqual
on the secondary field and pass the primary field's name to the directive
like this ngValidateEqual="primaryFieldName"
'notEqual'
error in the confirmation field's errors array
like this modelConfirmPassword.hasError('notEqual')
<!-- form -->
<form>
<!-- password -->
<label>
Password
</label>
<input type="password" name="passwordFieldName" placeholder="Password" #modelPassword="ngModel" [(ngModel)]="model.password">
<!-- confirm Password -->
<label>
Confirm Password
</label>
<input type="password" ngValidateEqual="passwordFieldName" #modelConfirmPassword="ngModel" [(ngModel)]="model.confirmPassword" placeholder="Confirm Password">
<div *ngIf="(modelConfirmPassword.dirty || modelConfirmPassword.touched) && modelConfirmPassword.invalid">
<p class="warning-text" *ngIf="modelConfirmPassword.hasError('notEqual') && modelPassword.valid">
Passwords Don't Match
</p>
</div>
</form>