For a Angular 2 application, I wrote a custom validator TypeScript class like below,
import { FormControl } from '@angular/forms';
export class AlphaNumericValidator {
static invalidAlphaNumeric(control: FormControl): { [key: string]: boolean } {
if (control.value.length && !control.value.match(/^[a-z0-9]+$/i)) {
return { invalidAlphaNumeric: true };
}
return null;
}
}
I am applying this validator for a Model Driven Form,
'name': [this.exercise.name, [Validators.required, AlphaNumericValidator.invalidAlphaNumeric]],
And Here is the HTML,
I notice that whenever I am typing a character in input, the above TypeScript class code called, but every time it's return Null.
Is there any issue on typeScript class?
Thanks!
No comments:
Post a Comment