URL Validation In JavaScript
noothan
Posted on September 20, 2023
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.");
}
💖 💪 🙅 🚩
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.