alphaNumericCharactersValidator static method

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

alphaNumericCharactersValidator to validate if a form field contains only alpha-numeric values

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

Implementation

static String? alphaNumericCharactersValidator(String? value,
    [String? errorMessage]) {
  RegExp regex = CustomRegEx.alphaNumericRegex;

  if (value == null || value.isEmpty) {
    return errorMessage ?? 'Required';
  } else if (!regex.hasMatch(value)) {
    return errorMessage ?? 'Only alpha-numeric characters are allowed';
  }

  return null;
}