postalCode static method

String? Function(String?) postalCode({
  1. String? message,
})

Validates postal code

Implementation

static String? Function(String?) postalCode({String? message}) {
  return (String? value) {
    if (value == null || value.trim().isEmpty) {
      return null;
    }

    final cleaned = value.replaceAll(RegExp(r'[\s-]'), '');

    // Postal code is usually 5 digits
    if (cleaned.length != 5) {
      return message ?? _defaultMessages['postalCode'];
    }

    // Validate only numbers
    if (!RegExp(r'^[0-9]+$').hasMatch(cleaned)) {
      return message ?? _defaultMessages['postalCode'];
    }

    return null;
  };
}