summaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-ui/src/app/paletx/plx-text-input/ipv4-validator.directive.ts
blob: 312ea5f3ce7ae9a8338af9dbe5646f11b5168dd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import {Directive, forwardRef} from '@angular/core';
import {AbstractControl, NG_VALIDATORS, Validators} from '@angular/forms';

@Directive({
  selector: '[ipv4][ngModel],[ipv4][formControl],[ipv4][formControlName]',
  providers: [{
	provide: NG_VALIDATORS,
	useExisting: forwardRef(() => Ipv4ValidatorDirective),
	multi: true
  }],
})

export class Ipv4ValidatorDirective {
  validate(c: AbstractControl) {
	if (Validators.required(c) !== undefined &&
		Validators.required(c) !== null) {
		return null;
	}
	const ipv4Reg =
		/^((25[0-5]|2[0-4]\d|[0-1]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[0-1]?\d\d?)$/;
	let regex = new RegExp(ipv4Reg);
	return regex.test(c.value) ? null : {'ipv4': true};
  }
}