Navigate back to the homepage

How to check if a string is valid url in javascript?

Infinitbility
JavaScript
June 1st, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, I’m going to show you, how do you check if a string is a valid URL in javascript, here I will use the regex expression to validate URL string.

To validate the string URL, I will use the below regex which can handle the below cases.

  1. protocol
  2. Domain name address
  3. IP (v4) address
  4. port and path
  5. URL query string
  6. fragment locator
1function isValidURL(str) {
2 var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
3 '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
4 '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
5 '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
6 '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
7 '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
8 return !!pattern.test(str);
9}

Let’s start the today’s tutorial title How do I check if a string is a valid URL in javascript?

Here, we will use the above-mentioned function isValidURL() to check if the string URL is valid or not, to use the above function we just pass out the URL in an isValidURL() method and it will return true or false based on valid ur string.

Let’s understand with an example.

1function isValidURL(str) {
2 var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
3 '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
4 '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
5 '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
6 '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
7 '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
8 return !!pattern.test(str);
9}
10
11console.log(isValidURL('http://infinitbility.com/')); // 👉️ true
12console.log(isValidURL('httpe://infinitbility.com/')); // 👉️ false
13
14if (isValidURL('http://www.infinitbility.com/')) {
15 console.log('✅ Valid URL')
16} else {
17 console.log('⛔️ Invalid URL')
18}

When running the above program it should log which URL address is valid and which is not. let’s check the output.

javascript, check if a string is a valid url example
javascript, check if a string is a valid url example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to check if number is integer or float in javascript?

Example of check if number is integer or float in javascript with step by step guide.

June 1st, 2022 · 1 min read

How to check if the first letter of a string is uppercase in javascript?

Example of check if the first letter of a string is uppercase in javascript with step by step guide.

May 31st, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]