Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to format the url?

I want to format url. Here is an example:

Input:

www.google.com

or

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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";
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading