timeInputValidator static method

String? timeInputValidator(
  1. String? input
)

Validates time input format and values

Checks if the input represents a valid time in 24-hour format.

Validation Rules:

  • Must contain 3-4 digits (e.g., "930" or "1030")
  • Hours must be 00-23
  • Minutes must be 00-59

Examples:

  • "1030" → null (valid)
  • "2530" → "Invalid time" (25 hours)
  • "1070" → "Invalid time" (70 minutes)
  • "12" → "format: HrMn" (too short)

input - Input string to validate Returns null if valid, error message if invalid

Implementation

static String? timeInputValidator(String? input) {
  if (input == null || input.isEmpty) {
    return 'Empty';
  }

  // Remove all non digits characters
  input = _removeNonDigits(input);

  if (input.length < 3 || input.length > 4) {
    return 'HHMM';
  }

  int hours;
  int minutes;

  if (input.length == 3) {
    // Deterministic HH:M0 interpretation.
    hours = int.parse(input.substring(0, 2));
    minutes = int.parse(input[2]) * 10;
  } else {
    // Standard HH:MM interpretation.
    hours = int.parse(input.substring(0, 2));
    minutes = int.parse(input.substring(2, 4));
  }

  if (hours > 23 || minutes > 59) {
    return 'Invalid time';
  }
  return null;
}