Forbidden Names

forbiddenUsernames: string[] = ['Chris', 'Anna'];
ngOnInit(): void {
  this.signupForm = new FormGroup({
    userData: new FormGroup({
      username: new FormControl('', [
        Validators.required,
        this.forbiddenNames.bind(this),
      ]),
    }),
  });
}
forbiddenNames(control: FormControl): ValidationErrors | null {
  if (this.forbiddenUsernames.indexOf(control.value) !== -1) {
    return { nameIsForbidden: true };
  }
  return null;
}
<span *ngIf="username?.errors?.['nameIsForbidden']">
  This username is forbidden!
</span>
Was this page helpful?