URL Validation in HTML: What type="url" Does and Does Not Check

Learn what HTML URL inputs validate, add an HTTPS rule with the Constraint Validation API, and handle internationalized domains and server-side checks.

Published
Reading time
6 min read
URL Validation in HTML: What type="url" Does and Does Not Check
On this page

HTML input type url validation can tell users that an entered address is malformed, but it cannot tell you whether a website exists or is safe to visit. This tutorial shows how to use the browser’s built-in checks, add an HTTPS-only rule, and decide what still belongs on the server.

Key Takeaways

  • type="url" checks the format of a nonempty value; add required if an address must be provided.
  • A valid URL is not necessarily an HTTP or HTTPS website address, a reachable destination, or a safe one.
  • Use the Constraint Validation API to inspect validity and present feedback, then apply your application’s rules on the server too.
  • Test internationalized domain names and browser behavior in the environments you support.

What HTML input type url validation checks

A URL input expects an absolute URL, including a scheme. For example, https://example.com is a website URL, while example.com lacks a scheme and fails the built-in format check. A relative path such as /contact also fails.

The URL type does not limit the scheme to HTTP or HTTPS. For example, mailto:person@example.com can be a valid URL but is not a website address. If your form accepts only websites, add an explicit scheme policy.

An empty URL input is valid unless it is also required. The browser can expose a missing required value through validity.valueMissing and a malformed nonempty URL through validity.typeMismatch. Browsers may provide URL-oriented keyboards or other input conveniences, but these are not security controls.

Most importantly, format validation makes no network request. It does not check DNS, connect to the host, verify a page exists, or confirm that the URL is safe. A syntactically valid address can point to a nonexistent or inaccessible destination.

Advertisement

Build a form with an HTTPS rule

The following standalone example uses native URL validation and the Constraint Validation API. Save it as form.html and open it in a browser. It demonstrates validation only: it does not send the address to a server.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Website address validation</title>
</head>
<body>
  <form id="website-form" novalidate>
    <label for="website">Website address (HTTPS required)</label>
    <input id="website" name="website" type="url" required
           placeholder="https://example.com">
    <button type="submit">Check address</button>
    <p id="result" role="status"></p>
  </form>

  <script>
    const form = document.querySelector('#website-form');
    const website = document.querySelector('#website');
    const result = document.querySelector('#result');

    function updateCustomValidity() {
      // Remove any previous custom error before checking the current value.
      website.setCustomValidity('');

      // Let the browser report missing or malformed values.
      if (website.validity.valueMissing || website.validity.typeMismatch) {
        return;
      }

      try {
        const url = new URL(website.value);
        if (url.protocol !== 'https:' || !url.hostname) {
          website.setCustomValidity('Enter an HTTPS website address.');
        }
      } catch {
        website.setCustomValidity('Enter a valid HTTPS website address.');
      }
    }

    website.addEventListener('input', () => {
      result.textContent = '';
      updateCustomValidity();
    });

    form.addEventListener('submit', (event) => {
      event.preventDefault();
      result.textContent = '';
      updateCustomValidity();

      if (!form.reportValidity()) return;

      result.textContent = 'The address passed the form checks.';
    });
  </script>
</body>
</html>

The form uses novalidate so the submit handler can update the custom rule before calling reportValidity(). That method checks the form and asks the browser to show validation feedback. checkValidity() checks constraints too, but does not itself request the browser’s validation UI. The native message and presentation vary by browser.

Try these inputs:

InputExpected result
EmptyFails because the field is required.
example.comFails the URL format check because it lacks a scheme.
mailto:person@example.comPasses the URL format check but fails the HTTPS rule.
https://example.comPasses these checks; this does not prove the destination is reachable.

The example clears the previous custom error before each check. Without that step, a corrected value can remain invalid because the old custom error is still set. In a real form, replace the demonstration submit handler with your submission workflow and validate the received value again on the server.

Advertisement

Internationalized domains and missing schemes

Website addresses can contain internationalized domain names, such as https://bücher.example. URL processing can represent a Unicode hostname in ASCII using an internationalized domain name encoding: for example, JavaScript’s new URL('https://bücher.example').hostname yields xn--bcher-kva.example in conforming implementations. Test the input, display, and server-side handling in your supported browsers and backend stack; do not assume every component stores or compares the same textual representation.

If users commonly enter example.com, decide explicitly whether to require a scheme or offer to add https://. If you normalize an entry, show users the resulting address and validate it afterward. Do not silently assume that every destination supports HTTPS. Also avoid comparing raw URL strings as a substitute for parsing and applying your hostname policy.

Server-side validation and reachability

Client-side checks improve feedback, but a request can bypass the form entirely. On the server, parse the submitted value, handle parsing errors, enforce the allowed schemes and any application-specific hostname rules, and reject values that do not meet your policy. Apply authentication and authorization as appropriate.

If the server fetches the submitted URL, validation is also an SSRF security boundary. Restrict destinations and schemes; account for loopback, private, link-local, and other prohibited addresses; resolve and validate addresses at connection time to reduce DNS rebinding risk; and revalidate every redirect. Use network egress restrictions as a separate layer, and set timeouts and response-size limits. A URL parser or hostname check alone is not a complete defense.

If your product needs a reachability check, perform it separately from syntax validation and explain what it actually means. DNS failures, timeouts, redirects, authentication, and temporary outages can all affect the result; a successful check at one moment does not guarantee later availability or safety.

Common Mistakes

  • Assuming type="url" means HTTPS. It accepts URL schemes beyond website protocols; enforce your own policy.
  • Forgetting required. An empty optional URL field passes its URL-format constraint.
  • Treating a valid format as proof of a live website. No connection is made by the built-in check.
  • Relying only on browser validation. Direct requests can bypass it.
  • Leaving a custom error set. Clear or update setCustomValidity() when the value changes.
  • Using an online or client-side parser as an SSRF defense. Server-side destination controls and network restrictions are still necessary.

Best Practices

Provide a visible label and an example with a scheme. Explain any HTTPS-only requirement before submission, and give users actionable feedback when a value fails. Test empty values, missing schemes, non-web schemes, internationalized domains, and your custom rules in each supported browser.

Documentation retrieval and browser execution were unavailable for this article, so the example has not been run here. Check the current MDN documentation and HTML Living Standard, and test the form in your target browsers before deployment.

Conclusion

Use type="url" for useful built-in format feedback, then add the application-specific rules it cannot express. Validate again on the server, and treat any server-side URL fetching as a separate security problem.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.