JavaScript - Validate URL in JS Last Updated : 16 Jan, 2025 Comments Improve Suggest changes Like Article Like Report We will explore different approaches to validating URLs in JavaScript. These approaches include using regular expressions (regex), the URL constructor, and various npm packages. let's see one by one.All valid URLs follow a particular pattern. They have three main parts, which are: ProtocolDomain name (or IP address) Port and path.1. Using Regular ExpressionTo validate a URL in JavaScript using a regular expression (regex), we will use the following pattern to match the common URL format in the below example. JavaScript function isValid(url) { const pattern = /^(https?:\/\/)?([\w-]+\.)+[\w-]+(\/[\w-]*)*$/; return pattern.test(url); } //Driver Code Starts console.log(isValid("https://www.example.com")); console.log(isValid("http://example.com")); console.log(isValid("www.example.com")); console.log(isValid("invalid-url")); //Driver Code Ends Outputtrue true true false Explanation of the Regular Expression we used: ^(https?:\/\/)?: Matches an optional http:// or https://.([\da-z.-]+): Matches the domain name (alphanumeric characters, dots, and hyphens).\.: Matches the dot before the domain extension.([a-z.]{2,6}): Matches the domain extension (For ex., .com, .org).([\/\w .-]*)*\/?: Matches the optional path, query strings, or fragments.2. Using URL ObjectThe URL object in JavaScript provides a built-in way to parse and validate URLs. It is robust, handles complex cases, and doesn't require writing or maintaining custom regular expressions. JavaScript function isValid(url) { try { new URL(url); return true; } catch (e) { return false; } } //Driver Code Starts console.log(isValid("https://www.example.com")); console.log(isValid("http://example.com")); console.log(isValid("www.example.com")); console.log(isValid("invalid-url")); //Driver Code Ends Outputtrue true false false 3. Using npm PackagesThere are two npm packages is-url and is-url-http to validate URL. Install the packages with the following command:npm install is-urlnpm install is-url-httpUsing is-url npm Packages JavaScript import isUrl from 'is-url'; console.log(isUrl("https://www.example.com")); console.log(isUrl("http://example.com")); console.log(isUrl("www.example.com")); console.log(isUrl("invalid-url")); Outputtruetruefalsefalse4. Using is-url-http npm Packages JavaScript import isUrlHttp from 'is-url-http'; console.log(isUrlHttp("https://www.example.com")); console.log(isUrlHttp("http://example.com")); console.log(isUrlHttp("www.example.com")); console.log(isUrlHttp("invalid-url")); Outputtruetruefalsefalse Comment More infoAdvertise with us Next Article JavaScript - Validate URL in JS P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-RegExp Similar Reads How to Validate XML in JavaScript ? Validation of XML is important for ensuring data integrity and adherence to XML standards in JavaScript. There are various approaches available in JavaScript using which validation of the XML can be done which are described as follows: Table of Content Using DOM ParserUsing Tag MatchingUsing DOM Par 2 min read How to validate URL in ReactJS? URL (Uniform Resource Locator) is a reference/address to a resource on the Internet. For example, www.geeksforgeeks.com is a URL. The following example shows how to validate the user entered data and check whether it is valid or not using the npm module in the ReactJS application. Creating React Ap 2 min read JavaScript Form Validation JavaScript form validation checks user input before submitting the form to ensure it's correct. It helps catch errors and improves the user experience.What we are going to CreateIn this article, we will guide you through creating a form validation application. This application will validate essentia 10 min read Validate a password using HTML and JavaScript Validating a password using HTML and JavaScript involves ensuring that user-entered passwords meet certain criteria, such as length, complexity, or character types (e.g., uppercase, lowercase, numbers, and symbols). This process enhances security by enforcing strong password requirements before form 2 min read How to Validate Decimal Numbers in JavaScript ? Validating user input is an essential aspect of Web Development. As a developer, when we are playing with the numeric inputs provided by the end-user, it is quite important to ensure that the input provided by the user is in the correct format. We can use the regular expression to Validate Decimal N 2 min read Like