username.validator.ts 441 Bytes
Newer Older
Trần Anh Phú's avatar
Trần Anh Phú committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import { AbstractControl, ValidationErrors } from '@angular/forms';
import { userNameRegex } from '../../data-access/const/user-name-regex.const';

export function validateUsername(
  usernameControl: AbstractControl
): ValidationErrors | null {
  const value = usernameControl.value;
  const userName = new RegExp(userNameRegex);
  if (value && !userName.test(value)) {
    return {
      userNamePattern: true,
    };
  }
  return null;
}