Dart / Flutter - Validating a string for URL
var urlPattern = r"(https?|http)://([-A-Z0-9.]+)(/[-A-Z0-9+&@#/%=~_|!:,.;]*)?(\?[A-Z0-9+&@#/%=~_|!:,.;]*)?";
var match = new RegExp(urlPattern, caseSensitive: false).firstMatch('https://www.google.com');
You can use RegExp too.
To check Valid URL string you just have to use Uri.parse()
like below.
bool _validURL = Uri.parse(_adVertData.webLink).isAbsolute;
Just check value of _validURL
Uri.tryParse(mystring)?.hasAbsolutePath ?? false;
Some example results:
url | result |
---|---|
'https://stackoverflow.com/questions/52975739/dart-flutter-validating-a-string-for-url' |
true |
asd |
false |
asd:asd |
false |
%EMPTY_STRING% |
false |
google.nl |
false |
https: |
false |
https:// |
false |
https://a |
false |
https://a/ |
true |