Can not contain space

ngOnInit(): void {
  this.signupForm = new FormGroup({
    userData: new FormGroup({
      username: new FormControl('', [
        Validators.required,
        this.cannotContainSpace.bind(this),
      ]),
    }),
  });
}
cannotContainSpace(control: AbstractControl): ValidationErrors | null {
  if ((control.value as string).indexOf(' ') >= 0) {
    return { cannotContainSpace: true };
  }
  return null;
}
<span *ngIf="username?.errors?.['cannotContainSpace']">
  The username can not contain space!
</span>
Was this page helpful?