source: trip-planner-front/src/app/providers/CustomValidators.ts

Last change on this file was b738035, checked in by Ema <ema_spirova@…>, 3 years ago

signup/login server errors on front and remove location from planner

  • Property mode set to 100644
File size: 1.1 KB
Line 
1import {
2 AbstractControl,
3 ValidatorFn,
4 FormGroup
5 } from '@angular/forms';
6
7export class CustomValidators {
8 constructor() {}
9
10 static onlyChar(): ValidatorFn {
11 return (control: AbstractControl): { [key: string]: boolean } | null => {
12 if (control.value == '') return null;
13
14 let re = new RegExp('^[a-zA-Z ]*$');
15 if (re.test(control.value)) {
16 return null;
17 } else {
18 return { onlyChar: true };
19 }
20 };
21 }
22
23 static mustMatch(controlName: string, matchingControlName: string) {
24 return (formGroup: FormGroup) => {
25 const control = formGroup.controls[controlName];
26 const matchingControl = formGroup.controls[matchingControlName];
27
28 if (matchingControl.errors && !matchingControl.errors.mustMatch) {
29 return;
30 }
31
32 // set error on matchingControl if validation fails
33 if (control.value !== matchingControl.value) {
34 matchingControl.setErrors({ mustMatch: true });
35 } else {
36 matchingControl.setErrors(null);
37 }
38 return null;
39 };
40 }
41
42 }
Note: See TracBrowser for help on using the repository browser.