import { ValidatorFn, AbstractControl } from '@angular/forms/src/forms'; import { FormGroup, FormControl } from '@angular/forms/forms'; export function maxValue(max: Number): ValidatorFn { return (control: AbstractControl): { [key: string]: any } => { const input = control.value, isValid = input > max; if (isValid) { return { 'maxValue': { max } } } else { return null; } }; } export function minValue(min: Number): ValidatorFn { return (control: AbstractControl): { [key: string]: any } => { const input = control.value, isValid = input < min; if (isValid) { return { 'minValue': { min } } } else { return null; } }; } export function matchOtherValidator(otherControlName: string) { let thisControl: FormControl; let otherControl: FormControl; return function matchOtherValidate(control: FormControl) { if (!control.parent) { return null; } // Initializing the validator. if (!thisControl) { thisControl = control; otherControl = control.parent.get(otherControlName) as FormControl; if (!otherControl) { throw new Error('matchOtherValidator(): other control is not found in parent group'); } otherControl.valueChanges.subscribe(() => { thisControl.updateValueAndValidity(); }); } if (!otherControl) { return null; } if (otherControl.value !== thisControl.value) { return { matchOther: true }; } return null; } }