UNPKG

@the-node-forge/url-validator

Version:

A lightweight and efficient library for validating URLs. It ensures that URLs are correctly formatted and provides an optional feature to check whether a URL is live by sending an HTTP request. This package is designed for web applications, APIs, and serv

29 lines (28 loc) 851 B
export function validateUrl(url) { try { const parsedUrl = new URL(url); // Ensure valid protocols (http/https) const validProtocols = new Set(['http:', 'https:']); if (!validProtocols.has(parsedUrl.protocol)) { return false; } // Check for valid domain/TLD const domainPattern = /^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; if (!domainPattern.test(parsedUrl.hostname)) { return false; } // Reject malformed IP addresses const ipPattern = /^(?:\d{1,3}\.){3}\d{1,3}$|^\[?[a-fA-F0-9:]+\]?$/; if (ipPattern.test(parsedUrl.hostname)) { return false; } // Reject spaces in URL if (url.includes(' ')) { return false; } return true; } catch { return false; } }