My web application has an input field where users can enter a link to their website. Usually they enter invalid URLs such as e-918kiss.com. I want to fix it to https://e-918kiss.com/ automatically.
But the URL can point to any domain name.
I tried using a tag to parse the URL, but it just appended it as the local path of the current source:
const elem = document.createElement('a');
a.href = 'Twitter.com/mhluska';
console.log(a.href); // "http://e-918kiss.com"
I researched some URL parsing libraries, but they usually just throw errors for invalid URLs. Includes native URL API.
Is there an easy way to try to create a valid link from any junk that the user might enter 918kiss ?