0

I am trying to make an app where I can allow people to add different company domains.

I am trying to use a React input field.

How can I validate that a domain has been entered. I tried setting the type to url but it does not work for domains.

Valid Domain: company.com

URL: http://company.com

Is there an easy way to check whether the domain has been entered. Thank you!

skyboyer
  • 22,209
  • 7
  • 57
  • 64
  • What are your tries? Where is your research? – Leandro Bardelli Nov 25 '21 at 20:35
  • Does this answer your question? [What is a good regular expression to match a URL?](https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url) Use Regex. – code Nov 25 '21 at 20:35

1 Answers1

0

You should use the input type url so the form won't submit unless it is a url. You also need some JS to validate it.

function myFunction() {
  const url = document.getElementById("url9");
  if (!url.checkValidity()) {
     document.getElementById("demo").innerHTML = "Insert a URL";
  } 
} 
<input type="url" id="url9">
<button onclick="myFunction()">Check</button>
<p id="demo"></p>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Varun W.
  • 242
  • 2
  • 14