date static method

Validator date()

Implementation

static Validator date() {
  return (String? input) {
    // Remove any non-numeric characters from the input
    String? numericInput = input!.replaceAll(RegExp(r'\D'), '');

    // Validate the length of the numeric input
    if (numericInput.length != 8) {
      return 'Invalid date';
    }

    // Extract the day, month, and year components
    int? day = int.tryParse(numericInput.substring(0, 2));
    int? month = int.tryParse(numericInput.substring(2, 4));
    int? year = int.tryParse(numericInput.substring(4, 8));
    // Validate the day, month, and year components
    if (day == null || month == null || year == null) {
      return 'Invalid date';
    }

    if (day > 29 && month == 2) {
      return 'Invalid February date';
    }

    // Validate the date components
    if (day < 1 || day > 31 || month < 1 || month > 12 || year < 1900) {
      return 'Invalid date';
    }

    // Ensure that the year is not less than 5 years ago
    DateTime now = DateTime.now();
    DateTime inputDate = DateTime(year, month, day);
    if (inputDate.isAfter(now.subtract(const Duration(days: 365 * 5)))) {
      return 'You must be at least 5 years old to register.';
    }

    // Additional validation logic can be added if needed

    return null;
  };
}