Url static method

String? Function(String? value)? Url({
  1. String errorMessage = 'Invalid url',
  2. String? next(
    1. String value
    )?,
})

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;
  };
}