usernameValidator static method

String? usernameValidator(
  1. String? value, [
  2. String? errorMessage
])

usernameValidator to validate a username

validator: (value) => SimpleValidations.usernameValidator(value, [errorMessage]),

Implementation

static String? usernameValidator(String? value, [String? errorMessage]) {
  RegExp regex = CustomRegEx.usernameRegex;
  if (value == null || value.isEmpty) {
    return errorMessage ?? 'Required';
  } else if (value.length < 3 || value.length > 16) {
    return errorMessage ??
        'Username must be between 3 and 16 characters long';
  } else if (!regex.hasMatch(value)) {
    return errorMessage ??
        'Username can only contain letters, numbers, and underscores';
  }
  return null;
}