Monday, 22 May 2017

angular - alpha numeric validation is not working

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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...