|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { AbstractControl, FormBuilder, FormControl, FormGroup, ValidationErrors, ValidatorFn, Validators } from '@angular/forms'; |
| 3 | + |
| 4 | +interface User |
| 5 | +{ |
| 6 | + email: FormControl<string>; |
| 7 | + password: FormControl<string>; |
| 8 | + repeatPassword: FormControl<string>; |
| 9 | +} |
| 10 | + |
| 11 | +interface ValidatorErrors |
| 12 | +{ |
| 13 | + localPart?: boolean; |
| 14 | + domain?: boolean; |
| 15 | + containsEmail?: boolean; |
| 16 | + mismatch?: boolean; |
| 17 | +} |
| 18 | + |
| 19 | +@Component({ |
| 20 | + selector: 'app-reactive-form-custom-validation', |
| 21 | + templateUrl: './reactive-form-custom-validation.component.html', |
| 22 | + styleUrls: ['./reactive-form-custom-validation.component.scss'] |
| 23 | +}) |
| 24 | +export class ReactiveFormCustomValidationComponent { |
| 25 | + private pattern = `^(?=.*[A-Za-z])(?=.*\\d)(?=.*[~!@?#+$"'%^&:;*\\-_=.,<>])[A-Za-z\\d~!@?#+$"'%^&:;*\\-_=.,<>]+$`; |
| 26 | + public registrationForm: FormGroup<User>; |
| 27 | + public showPassword: boolean = false; |
| 28 | + public showRepeat: boolean = false; |
| 29 | + |
| 30 | + constructor(fb: FormBuilder) { |
| 31 | + this.registrationForm = fb.group({ |
| 32 | + email: ['', { |
| 33 | + nonNullable: true, |
| 34 | + validators: [ |
| 35 | + Validators.required, |
| 36 | + Validators.email, |
| 37 | + this.emailValidator('infragistics') |
| 38 | + ] |
| 39 | + }], |
| 40 | + password: ['', { |
| 41 | + nonNullable: true, |
| 42 | + validators: [ |
| 43 | + Validators.required, |
| 44 | + Validators.minLength(8), |
| 45 | + Validators.pattern(this.pattern) |
| 46 | + ] |
| 47 | + }], |
| 48 | + repeatPassword: ['', { |
| 49 | + nonNullable: true, |
| 50 | + validators: [Validators.required] |
| 51 | + }] |
| 52 | + }, |
| 53 | + { |
| 54 | + validators: [this.passwordValidator()] |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + public get email() { |
| 59 | + return this.registrationForm.get('email'); |
| 60 | + } |
| 61 | + |
| 62 | + public get password() { |
| 63 | + return this.registrationForm.get('password'); |
| 64 | + } |
| 65 | + |
| 66 | + public get repeatPassword() { |
| 67 | + return this.registrationForm.get('repeatPassword'); |
| 68 | + } |
| 69 | + |
| 70 | + public get togglePasswordVisibility() { |
| 71 | + return this.showPassword ? 'visibility_off' : 'visibility'; |
| 72 | + } |
| 73 | + |
| 74 | + public get toggleRepeatVisibility() { |
| 75 | + return this.showRepeat ? 'visibility_off' : 'visibility'; |
| 76 | + } |
| 77 | + |
| 78 | + public onSubmit() { |
| 79 | + console.log(this.registrationForm.value); |
| 80 | + this.registrationForm.reset(); |
| 81 | + } |
| 82 | + |
| 83 | + private emailValidator(val: string): ValidatorFn { |
| 84 | + return (control: AbstractControl): ValidationErrors | null => { |
| 85 | + const value = control.value?.toLowerCase(); |
| 86 | + const localPartRegex = new RegExp(`(?<=(${val})).*[@]`); |
| 87 | + const domainRegex = new RegExp(`(?<=[@])(?=.*(${val}))`); |
| 88 | + const returnObj: ValidatorErrors = {}; |
| 89 | + |
| 90 | + if (value && localPartRegex.test(value)) { |
| 91 | + returnObj.localPart = true; |
| 92 | + } |
| 93 | + |
| 94 | + if (value && domainRegex.test(value)) { |
| 95 | + returnObj.domain = true; |
| 96 | + } |
| 97 | + |
| 98 | + return returnObj; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private passwordValidator(): ValidatorFn { |
| 103 | + return (control: AbstractControl): ValidationErrors | null => { |
| 104 | + const email = control.get('email'); |
| 105 | + const password = control.get('password'); |
| 106 | + const repeatPassword = control.get('repeatPassword'); |
| 107 | + const returnObj: ValidatorErrors = {}; |
| 108 | + |
| 109 | + if (email.value |
| 110 | + && password.value |
| 111 | + && password.value.toLowerCase().includes(email.value)) { |
| 112 | + password.setErrors({ ...password.errors, containsEmail: true }); |
| 113 | + returnObj.containsEmail = true; |
| 114 | + } |
| 115 | + |
| 116 | + if (password |
| 117 | + && repeatPassword |
| 118 | + && password.value !== repeatPassword.value) { |
| 119 | + repeatPassword.setErrors({ ...repeatPassword.errors, mismatch: true }); |
| 120 | + returnObj.mismatch = true; |
| 121 | + } |
| 122 | + |
| 123 | + if (!returnObj.containsEmail && password.errors?.containsEmail) { |
| 124 | + password.setValue(password.value); |
| 125 | + } |
| 126 | + |
| 127 | + if (!returnObj.mismatch && repeatPassword.errors?.mismatch) { |
| 128 | + repeatPassword.setValue(repeatPassword.value); |
| 129 | + } |
| 130 | + |
| 131 | + return returnObj; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments