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.
Using
[validator][ngModel]
will do the trick in your case as you don't need to have an input for this directiveLike this
Add this directive to
declarations
array ofNgmodule
ValidatorDirective,
The working example of the same link.