1

So I have an input element in HTML, that input element asks the user for his website's address/name.

Now here's the thing I accept the following address/name formats:

  • https://example.com (.net, .org, etc.)
  • http://example.com (.net, .org, etc.)
  • www.example.com (.net, .org, etc.)
  • http://www.example.com (.net, .org, etc.)
  • https://www.example.com (.net, .org, etc.)
  • example.com (.net, .org, etc.)

But If the user types a random string for example, abc123$ It would give an error "Please enter a valid website address/name".

What I tried so far,

const isValidURL = (str) => {
  var a = document.createElement("a");
  a.href = str;
  return a.host && a.host != window.location.host;
}

let isValid = isValidURL("https://example.com");

if (isValid == true) {
  // pass
} else if ...

Now, this code I found on an SO answer checks to see If it is a valid URL, i.e. contains http or https which is part of what I want. But after this in an else if statement I would also want to allow URLs that don't contain http or https but contain www. and then in another else if statement I would want to allow URLs that contain neither of those and just example.com (.net, .org, etc.), but THEN an else statement which tells the user "Please enter a valid website address/name".

But the problem is that I have no idea how I will do this, I would appreciate it if someone were to shed some light upon this.

Note: I do know that checking the URL on the backend is the best thing to do but sadly due to client requirements I need to use this client-side method of the bare-minimum sitename validation.

Edit: This is not a duplicate of other such questions because I am not just trying to validate the URLs, I am trying to validate all the possible domain name types valid on a web browser. See the examples I presented above, not all of them are valid URLs the ones that aren't should pass too, but If it is not a valid site name entirely like "example" instead of "example.com, www.example.com, or http://example.com, etc." then It doesn't pass.

Syed M. Sannan
  • 1,061
  • 2
  • 9
  • 28
  • 1
    It sounds like you already know how to validate URLs beginning with a scheme (http/https) and you'd like to know how to also validate the domain name part (example.com). Could you edit your question to be more specific? I suggest you remove all the http/https examples, since you already know how to handle those. – alexanderbird Jan 02 '22 at 22:06
  • Anyway, what is “not working” with the code shown? It’s a good start. – user2864740 Jan 02 '22 at 22:06
  • @charlietfl it's fine for user input, user doesn't have to type a protocol, which can be added on server side – flppv Jan 02 '22 at 22:49
  • @user2864740 Nothing not working, I would like to know how to make the domain name validation. – Syed M. Sannan Jan 03 '22 at 07:20
  • @charlietfl Yes Charlie, I know It isn't, but I don't specifically want to validate URLs please read my question as I am accepting any website domain types a user might enter (`example.com` or `https://example.com` or even `www.example.com` *BUT NOT* `example` it must contain an ending .text like .com, .net, etc. but It cannot be a simple plain text like "example" It must suit all ways that make a valid website address when typing on Chrome). – Syed M. Sannan Jan 03 '22 at 07:23
  • @flppv Please read my question carefully, I am not solely trying to check If it is a URL or not I am actually trying to validate all possible website name combinations as I mentioned earlier, example.com, www.example.com, http://www.example.com, etc. should all be considered valid URLs but NOT example. – Syed M. Sannan Jan 03 '22 at 07:49
  • Yes this one answers my question, thanks. – Syed M. Sannan Jan 03 '22 at 08:57

0 Answers0