I created the directive for validating the input value. I need that on input some characters my input/form become ivalid. Here is my code, but it is not working.
import {Directive, Input} from '@angular/core';
import {AbstractControl, NG_VALIDATORS, ValidationErrors} from '@angular/forms';
const regExp = new RegExp('(>|<)+');
@Directive({
selector: '[validator]',
providers: [{
provide: NG_VALIDATORS,
useExisting: ValidatorDirective,
multi: true
}]
})
export class ValidatorDirective {
@Input('validator') input;
constructor() {
}
validate (control: AbstractControl): ValidationErrors {
const value = control.value;
const isValid = regExp.test(value);
return isValid ? null : {
validator: {
valid: false
}
};
}
}
Thank you for your help. Have a nice day.
[validator][ngModel]get the value fromAbstractControl