CyrefJS
CCyrefJS
فا
Back to documentation
Validation/Since 1.38.0

isURL

Checks whether a value is a valid URL using the native URL constructor and allows only HTTP and HTTPS protocols.

isURL(value):boolean

Validation

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

valueunknownrequired

The value to validate as a URL.

Returns

boolean

Returns true when the value can be parsed as a URL with an HTTP or HTTPS protocol; otherwise returns false.

Examples

01

Valid HTTP URL

HTTP URLs are accepted.

TypeScript
isURL("http://example.com");
// true
02

Valid HTTPS URL

HTTPS URLs are accepted.

TypeScript
isURL("https://example.com/path?query=1");
// true
03

Unsupported protocol

URLs using protocols other than HTTP or HTTPS are rejected.

TypeScript
isURL("ftp://example.com");
// false

isURL("mailto:test@example.com");
// false
04

Invalid URL

Values that cannot be parsed by the native URL constructor are rejected.

TypeScript
isURL("not-a-url");
// false

isURL("example.com");
// false
05

Whitespace

Leading and trailing whitespace is removed before validation.

TypeScript
isURL("  https://example.com  ");
// true
06

Non-string value

Non-string values are rejected.

TypeScript
isURL(123);
// false

isURL(null);
// false
Built with@cyref/js·Official documentation