312ea5f3ce7ae9a8338af9dbe5646f11b5168dd6
[sdc/sdc-workflow-designer.git] /
1 import {Directive, forwardRef} from '@angular/core';
2 import {AbstractControl, NG_VALIDATORS, Validators} from '@angular/forms';
3
4 @Directive({
5   selector: '[ipv4][ngModel],[ipv4][formControl],[ipv4][formControlName]',
6   providers: [{
7         provide: NG_VALIDATORS,
8         useExisting: forwardRef(() => Ipv4ValidatorDirective),
9         multi: true
10   }],
11 })
12
13 export class Ipv4ValidatorDirective {
14   validate(c: AbstractControl) {
15         if (Validators.required(c) !== undefined &&
16                 Validators.required(c) !== null) {
17                 return null;
18         }
19         const ipv4Reg =
20                 /^((25[0-5]|2[0-4]\d|[0-1]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[0-1]?\d\d?)$/;
21         let regex = new RegExp(ipv4Reg);
22         return regex.test(c.value) ? null : {'ipv4': true};
23   }
24 }