splitforms.com

Free tool · runs in your browser

URL regex tester

Test website URLs against a strict http(s) regex, a lenient pattern where the scheme is optional, and the browser's own new URL() check, one value per line. Then copy the JavaScript, the HTML pattern attribute or PHP filter_var code.

/^https?:\/\/[^\s/$.?#].\S*$/i

Needs http:// or https:// and a host. Rejects example.com, ftp:, mailto: and javascript:.

6 of 13 pass

ResultValuenew URL() returns
Failexample.comthrows TypeError
Failwww.example.comthrows TypeError
Passhttps://example.comhttps://example.com/
Passhttps://example.com/path?q=1https://example.com/path?q=1
Passhttp://localhost:3000http://localhost:3000/
Passhttps://sub.example.co.uk/a#tophttps://sub.example.co.uk/a#top
PassHTTPS://EXAMPLE.COMhttps://example.com/
Failftp://x.yftp://x.y/not http(s)
Failjavascript:alert(1)javascript:alert(1)not http(s)
Failmailto:hi@example.commailto:hi@example.comnot http(s)
Passhttps://例え.jphttps://xn--r8jz45g.jp/
Failhttps:// space.comthrows TypeError
Fail//example.comthrows TypeError

The HTML pattern has no ^ or $ and no i flag: browsers anchor it to the whole value and match case exactly.

Why type=url accepts javascript: and mailto:

An input type=url is valid when the value parses as an absolute URL, which is close to asking whether new URL(value) throws. Any scheme counts, so javascript:alert(1), mailto:hi@example.com and ftp://x.y all pass. In the tester, javascript:alert(1) fails the strict pattern but passes new URL().

That matters when you show the value as a link: a javascript: URL in an href runs code when clicked. So allow only http and https on the server, where the check can't be skipped:

is-http-url.js

function isHttpUrl(value) {
  try {
    const u = new URL(value.trim());
    return u.protocol === "http:" || u.protocol === "https:";
  } catch {
    return false; // new URL() throws on anything that isn't an absolute URL
  }
}
validate-url.php

$url = trim($_POST['website'] ?? '');
$scheme = strtolower((string) parse_url($url, PHP_URL_SCHEME));
$ok = filter_var($url, FILTER_VALIDATE_URL) !== false
    && in_array($scheme, ['http', 'https'], true);

The HTML website input

website.html

<label for="website">Website</label>
<input type="url" id="website" name="website" pattern="https?://.+" placeholder="https://">

type="url" gives phones a URL keyboard, and the pattern attribute adds the http(s) rule. Browsers anchor the pattern to the whole value, so it has no ^ or $, and it matches case exactly.

Prefill https:// or normalise on the server

Most people type example.com, which type=url rejects. There are two fixes. Prefill the field with https:// so the user types after it, or use a text input with the lenient pattern and add the scheme on the server before you store the value:

normalise.js

function normaliseWebsite(value) {
  const v = value.trim();
  if (v === "") return "";
  return /^[a-z][a-z\d+.-]*:/i.test(v) ? v : `https://${v}`; // "example.com" → "https://example.com"
}

Run the http(s) check after you normalise, not before. A URL that passes every check can still point to a site that doesn't exist; a regex checks format only.

URL regex FAQ

What is a good regex for a URL?

For a website field, ^https?:\/\/[^\s/$.?#].\S*$ with the i flag is a practical choice: it needs http:// or https://, a host and no spaces. No regex fully checks a URL, so for the final check parse it with new URL() and confirm the protocol is http: or https:.

Does input type=url block javascript: links?

No. type=url only checks that the value is an absolute URL, and javascript:alert(1), mailto:hi@example.com and ftp://x.y all are. Add pattern="https?://.+" in the browser, and on the server accept only the http and https schemes.

Should a website field accept example.com without https://?

People often type example.com, and type=url rejects it. Either prefill the field with https:// or use a text input with a scheme-optional pattern, then add https:// on the server before you store or link the value.

How do I validate a URL in PHP?

Use filter_var($url, FILTER_VALIDATE_URL), then read the scheme with parse_url($url, PHP_URL_SCHEME) and allow only http and https, because filter_var accepts other schemes too.