isURL method
Check if the string value
is a URL.
protocols
sets the list of allowed protocolsrequireTld
sets if TLD is requiredrequireProtocol
is abool
that sets if protocol is required for validationallowUnderscore
sets if underscores are allowedhostWhitelist
sets the list of allowed hostshostBlacklist
sets the list of disallowed hosts
Implementation
bool isURL(
String? value, {
List<String?> protocols = const <String?>['http', 'https', 'ftp'],
bool requireTld = true,
bool requireProtocol = false,
bool allowUnderscore = false,
List<String> hostWhitelist = const <String>[],
List<String> hostBlacklist = const <String>[],
}) {
if (value == null ||
value.isEmpty ||
value.length > _maxUrlLength ||
value.startsWith('mailto:')) {
return false;
}
final int port;
final String? protocol;
final String? auth;
final String user;
final String host;
final String hostname;
final String portStr;
final String path;
final String query;
final String hash;
// check protocol
List<String> split = value.split('://');
if (split.length > 1) {
protocol = shift(split).toLowerCase();
if (!protocols.contains(protocol)) {
return false;
}
} else if (requireProtocol == true) {
return false;
}
final String str1 = split.join('://');
// check hash
split = str1.split('#');
final String str2 = shift(split);
hash = split.join('#');
if (hash.isNotEmpty && RegExp(r'\s').hasMatch(hash)) {
return false;
}
// check query params
split = str2.split('?');
final String str3 = shift(split);
query = split.join('?');
if (query.isNotEmpty && RegExp(r'\s').hasMatch(query)) {
return false;
}
// check path
split = str3.split('/');
final String str4 = shift(split);
path = split.join('/');
if (path.isNotEmpty && RegExp(r'\s').hasMatch(path)) {
return false;
}
// check auth type urls
split = str4.split('@');
if (split.length > 1) {
auth = shift(split);
if (auth?.contains(':') ?? false) {
user = shift(auth!.split(':'));
if (!RegExp(r'^\S+$').hasMatch(user)) {
return false;
}
if (!RegExp(r'^\S*$').hasMatch(user)) {
return false;
}
}
}
// check hostname
hostname = split.join('@');
split = hostname.split(':');
host = shift(split).toLowerCase();
if (split.isNotEmpty) {
portStr = split.join(':');
try {
port = int.parse(portStr, radix: 10);
} catch (e) {
return false;
}
if (!RegExp(r'^[0-9]+$').hasMatch(portStr) || port <= 0 || port > 65535) {
return false;
}
}
if (!_ipValidator.isIP(host, 0) &&
!isFQDN(
host,
requireTld: requireTld,
allowUnderscores: allowUnderscore,
) &&
host != 'localhost') {
return false;
}
if (hostWhitelist.isNotEmpty && !hostWhitelist.contains(host)) {
return false;
}
if (hostBlacklist.isNotEmpty && hostBlacklist.contains(host)) {
return false;
}
return true;
}