I want to format url. Here is an example:
Input:
www.google.com
or
google.com
Output:
https://www.google.com/
I need to format the url because I’m using a validator function that needs to check if the text is a url. When I type www.google.com or google.com it says it’s not a url because it requires https at the beginning of the text.
My code:
validator: (value) {
if (value == null) {
return null;
}
if (Uri.parse(value).host.isEmpty) {
return "Please type a valid url";
}
},
Feel free to leave a comment if you need more information.
How to format the url? I would appreciate any help. Thank you in advance!
>Solution :
I have added some more conditions to your validator function :
validator: (value) => {
if (value == null) {
return null;
}
if (!value.startsWith("https://")) {
value = "https://" + value;
}
if (!value.startsWith("https://www.") && !value.startsWith("https://")) {
value = "https://www." + value.substring(8);
}
if (Uri.parse(value).host.isEmpty) {
return "Please type a valid url";
}
}