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
| Result | Value | new URL() returns |
|---|---|---|
| Fail | example.com | throws TypeError |
| Fail | www.example.com | throws TypeError |
| Pass | https://example.com | https://example.com/ |
| Pass | https://example.com/path?q=1 | https://example.com/path?q=1 |
| Pass | http://localhost:3000 | http://localhost:3000/ |
| Pass | https://sub.example.co.uk/a#top | https://sub.example.co.uk/a#top |
| Pass | HTTPS://EXAMPLE.COM | https://example.com/ |
| Fail | ftp://x.y | ftp://x.y/not http(s) |
| Fail | javascript:alert(1) | javascript:alert(1)not http(s) |
| Fail | mailto:hi@example.com | mailto:hi@example.comnot http(s) |
| Pass | https://例え.jp | https://xn--r8jz45g.jp/ |
| Fail | https:// space.com | throws TypeError |
| Fail | //example.com | throws 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:
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
}
}$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
<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:
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.
Regex patterns for form fields
Alphanumeric regex tester
Test letters and numbers, spaces, handles and Unicode patterns.
Email regex tester
Test email patterns against edge cases and your own list.
Phone number regex tester
Check numbers against E.164, US/Canada and digits-only patterns.
Password regex generator
Pick strength rules and copy the regex or HTML pattern.
ZIP code regex
US ZIP, ZIP+4, Canadian and UK postal code patterns.
Number regex tester
Test digits, decimals, money and thousands separators.
Date regex tester
Date formats, strict ranges and leap-year aware ISO patterns.
Name regex tester
Validate names with accents, hyphens and apostrophes.
Username regex tester
Test username length, separators and reserved names.
IP address regex tester
Validate IPv4, IPv6 and CIDR input with strict octets.
HTML pattern attribute
How browsers anchor and check a pattern on an input.
HTML required attribute
Stop a form from sending while a field is empty.
Validate a form with JavaScript
Use checkValidity, custom messages and your own rules.
Time regex tester
Validate 24-hour, AM/PM and ISO 8601 time formats.
Credit card regex tester
Match card brands by prefix and length, then run a Luhn check.
Hex color regex tester
Match hex, rgb() and hsl() colors with range checks and swatches.
UUID regex tester
Match UUID v4, v7 and GUIDs with version and variant checks.
Use your validation patterns as you assemble a form with our HTML contact form code or HTML form generator.
Free form tools
Related tools.
Browse all 25 free tools
Form builders & HTML
Email & DNS
Converters & encoders
Calculators & marketing