url static method

String? url(
  1. String? value, {
  2. String? message,
})

Validates URL format.

Implementation

static String? url(String? value, {String? message}) {
  if (value == null || value.isEmpty) {
    return null;
  }

  final urlRegex = RegExp(
    r'^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$',
    caseSensitive: false,
  );

  if (!urlRegex.hasMatch(value)) {
    return message ?? 'Please enter a valid URL (e.g., https://example.com)';
  }

  return null;
}