HTTP URL String Validator

1.0.3 · maintenance · verified Wed Apr 22

This package provides a focused utility function to determine if a given string adheres to the structural requirements of an HTTP or HTTPS URL. Currently at version 1.0.3, it is a stable, low-cadence library, primarily maintained for compatibility and reliability rather than frequent feature additions. Its core validation logic is built upon a robust regular expression initially developed by Diego Perini, which is well-regarded for its comprehensive coverage of various URL components. Unlike larger, more feature-rich URL parsing libraries, `is-valid-http-url` offers a lightweight and direct API specifically for string format validation, making it ideal for scenarios where only a quick check for valid HTTP/HTTPS URL syntax is needed without additional overhead.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use the `isUrl` function to validate various HTTP and HTTPS URL strings, showcasing both valid and invalid examples.

import isUrl from 'is-valid-http-url';

// Test cases for valid HTTP/HTTPS URLs
console.log('Valid URLs:');
console.log(`http://example.com: ${isUrl('http://example.com')}`);
console.log(`https://www.example.com/foo/?bar=baz&inga=42&quux: ${isUrl('https://www.example.com/foo/?bar=baz&inga=42&quux')}`);
console.log(`http://localhost:3000: ${isUrl('http://localhost:3000')}`);
console.log(`https://user:pass@host.com/path?query#hash: ${isUrl('https://user:pass@host.com/path?query#hash')}`);

console.log('\nInvalid URLs:');
// Test cases for invalid URLs
console.log(`example.com (missing scheme): ${isUrl('example.com')}`);
console.log(`http://foo.bar?q=Spaces should be encoded: ${isUrl('http://foo.bar?q=Spaces should be encoded')}`);
console.log(`ftp://example.com (non-http scheme): ${isUrl('ftp://example.com')}`);
console.log(`invalid-url: ${isUrl('invalid-url')}`);
console.log(`
Note: This validator checks syntax, not if the URL actually exists or is reachable.`);

view raw JSON →