Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. import { Directive, Attribute } from '@angular/core';
  2. import { NG_VALIDATORS, Validator, FormControl } from '@angular/forms';
  3. import { Subscription } from 'rxjs';
  4.  
  5. @Directive({
  6. selector: '[appPasswordvalidator]',
  7. providers: [
  8. {
  9. provide: NG_VALIDATORS,
  10. useClass: PasswordvalidatorDirective,
  11. multi: true
  12. }
  13. ]
  14. })
  15. export class PasswordvalidatorDirective implements Validator {
  16.  
  17. constructor(@Attribute('appPasswordvalidator') public PasswordControl: string) { }
  18.  
  19. validate(c: FormControl) {
  20.  
  21. const Password = c.root.get(this.PasswordControl);
  22. const ConfirmPassword = c;
  23.  
  24. if (ConfirmPassword.value === null) {
  25. return null;
  26. }
  27.  
  28. if (Password) {
  29. const subscription: Subscription = Password.valueChanges.subscribe(() => {
  30. ConfirmPassword.updateValueAndValidity();
  31. subscription.unsubscribe();
  32. });
  33. }
  34. return Password && Password.value !== ConfirmPassword.value ? { passwordMatchError: true } : null;
  35. }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement