Node.js Project to Validate Youtube Video URL and ID Using youtube-validator Library in Javascript Full Tutorial For Beginners

 

Welcome folks today in this blog post we will be validating youtube video url using youtube-validator library in javascript. All the full source code of application is shown below.

 

 

 

Get Started

 

 

 

In order to get started we need to install youtube-validator` library using the npm command

 

npm i youtube-validator

 

After installing this library just create an index.js file and copy paste the following code

 

index.js

 

var validator = require('youtube-validator')

var url = 'www.youtube.com/watc     h?v=2XH5_qafR8k'.toUpperCase()

validator.validateUrl(url, function(res, err) {
  if(err) //err
  {
    console.log(err)
   }
  else
    {
      console.log("video url is correct")
   }
})`

 

 

As you can see that the youtube video url is incorrect if we execute this node.js script you will see following error which is shown below

 

 

 

If you correct your url like as shown below

 

var validator = require('youtube-validator')

var url = 'www.youtube.com/watch?v=2XH5_qafR8k'

validator.validateUrl(url, function(res, err) {
  if(err) //err
  {
    console.log(err)
   }
  else
    {
      console.log("video url is correct")
   }
})

 

READ  Build a Javascript String Code Obfuscator Encoder in Browser Using Vanilla Javascript Full Project For Beginners

 

Now if you execute this node.js script it will be valid as shown below

 

 

 

Now we will be validating video id inside this application. Now copy paste the following code as shown below

 

var validator = require('youtube-validator')

var id = '2XH5_qafR8k'.toUpperCase()

validator.validateVideoID(id, function(res, err) {
  if(err) //err
  {
    console.log(err)
  }
  else{
    console.log("video id is correct")
  }
    //res == url
})

 

 

Leave a Reply