1.38.0isURL
Checks whether a value is a valid URL using the native URL constructor and allows only HTTP and HTTPS protocols.
isURL(value):booleanValidation
The function first checks that the value is a string, trims it, and rejects empty strings. It then passes the value to the native URL constructor.
Supported protocols
After parsing the URL, the function accepts only http: and https: protocols. Other valid URL protocols are rejected.
Invalid URLs
If the native URL constructor throws while parsing the value, the function catches the error and returns false.
Type guard
The function is a TypeScript type guard. When it returns true, TypeScript narrows the value from unknown to string.
Parameters
valueunknownrequiredThe value to validate as a URL.
Returns
booleanReturns true when the value can be parsed as a URL with an HTTP or HTTPS protocol; otherwise returns false.
Examples
Valid HTTP URL
HTTP URLs are accepted.
isURL("http://example.com");
// trueValid HTTPS URL
HTTPS URLs are accepted.
isURL("https://example.com/path?query=1");
// trueUnsupported protocol
URLs using protocols other than HTTP or HTTPS are rejected.
isURL("ftp://example.com");
// false
isURL("mailto:test@example.com");
// falseInvalid URL
Values that cannot be parsed by the native URL constructor are rejected.
isURL("not-a-url");
// false
isURL("example.com");
// falseWhitespace
Leading and trailing whitespace is removed before validation.
isURL(" https://example.com ");
// trueNon-string value
Non-string values are rejected.
isURL(123);
// false
isURL(null);
// false