validateIP function

String? Function(dynamic) validateIP({
  1. String? failMessage,
  2. bool iPv4 = true,
  3. bool iPv6 = true,
})

Returns error message if invalid, null if valid. By default both iPv4 and iPv6 are enabled. Set either to false to restrict which formats are accepted.

Implementation

String? Function(dynamic) validateIP({
  String? failMessage,
  bool iPv4 = true,
  bool iPv6 = true,
}) {
  return (dynamic value) {
    if (value == null || value.isEmpty) return null;
    if (!isIP(value, iPv4: iPv4, iPv6: iPv6)) {
      return failMessage ?? 'Please enter a valid IP address';
    }
    return null;
  };
}