URL Validation In JavaScript

noothan

noothan

Posted on September 20, 2023

URL Validation In JavaScript

URL Validation in JavaScript (2023 edition)

Introduction

For many years, there has been no easy way to validate URLs in JavaScript. However, with the introduction of the URL.canParse() method, this is no longer the case.

URL.canParse()

URL.canParse() is a static method on the URL constructor. It takes a URL string as input and returns true if the URL is valid and false otherwise.

To use URL.canParse(), simply pass the URL string to the method. For example:


javascript
const url = "https://www.stefanjudis.com/blog/validate-urls-in-javascript/";

if (URL.canParse(url)) {
  console.log("The URL is valid.");
} else {
  console.log("The URL is invalid.");
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
noothan
noothan

Posted on September 20, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

URL Validation In JavaScript
webdev URL Validation In JavaScript

September 20, 2023