isValidAlphanumericOTP static method

String? isValidAlphanumericOTP(
  1. String? otp, {
  2. int length = 6,
})

Validates if the input is a valid alphanumeric OTP of specified length

Implementation

static String? isValidAlphanumericOTP(String? otp, {int length = 6}) {
  if (otp == null || otp.isEmpty) {
    return 'Please enter the verification code.';
  }

  if (otp.length != length) {
    return 'Code must be $length characters.';
  }

  final alphanumericRegExp = RegExp(r'^[A-Za-z0-9]+$');
  if (!alphanumericRegExp.hasMatch(otp)) {
    return 'Code must contain only letters and numbers.';
  }

  return null;
}