Url static method
Returns a validator that checks if a string is a valid url.
Arguments :
errorMessage
: The error message to return if the string is not a valid url.next
: A validator to run after this validator.
Usage :
TextFormField(
validator: Validators.Url(),
),
Implementation
static String? Function(String? value)? Url({
String errorMessage = 'Invalid url',
String? Function(String value)? next,
}) {
return (value) {
if (value == null || value.trim().isEmpty) {
return null;
}
if (!RegExp(
r"^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}")
.hasMatch(value)) {
return errorMessage;
}
if (next != null) {
return next(value);
}
return null;
};
}