Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check the valid Youtube url using jquery

In Jquery i want to check the specific url from youtube alone and show success status and others i want to skip by stating it as not valid url

var _videoUrl = "youtube.com/watch?v=FhnMNwiGg5M";
if (_videoUrl.contains("youtube.com"))
{
  alert('Valid'); 
} 
else
{ 
  alert('Not Valid');
} 

how to check with contains. or any other option to check the valid youtube url alone.

like image 559
kart Avatar asked Feb 13 '10 08:02

kart


People also ask

How to check if a URL is a valid YouTube video link?

We can check if a URL is a valid YouTube video link or not easily in PHP using the parse_url () function. The parse_url is the inbuilt function of PHP that is supported by PHP 4 and all the higher version of PHP. Now in this tutorial, we are going to validate if a link or URL is a valid YouTube video URL or not.

How to validate YouTube URL with JavaScript regexes?

Since the url is a valid YouTube URL, the console log should log true. To validate YouTube URL with JavaScript regexes, we can use the following regex: /^ (?:https?:\/\/)? (?:www\.)? (?:youtu\.be\/|youtube\.com\/ (?:embed\/|v\/|watch\?v=|watch\?.+&v=)) ( (\w|-) {11}) (?:\S+)?$/

How to get the video id from the URL using JavaScript?

Given a Youtube Video URL, The job is to get the Video ID from the URL using JavaScript. Here are few methods discussed. This method is used to split a string into an array of substrings, and returns the new array. separator: This parameter is optional. It specifies the character, or the regular expression, to use for splitting the string.


3 Answers

I found this from the closure library, might be handy:

/**
 * A youtube regular expression matcher. It matches the VIDEOID of URLs like
 * http://www.youtube.com/watch?v=VIDEOID.
 * @type {RegExp}
 * @private
 */
goog.ui.media.YoutubeModel.matcher_ =
    /https?:\/\/(?:[a-zA_Z]{2,3}.)?(?:youtube\.com\/watch\?)((?:[\w\d\-\_\=]+&(?:amp;)?)*v(?:<[A-Z]+>)?=([0-9a-zA-Z\-\_]+))/i;
like image 81
David Hellsing Avatar answered Sep 27 '22 22:09

David Hellsing


Typically, the thing that most people want is the youtube video ID. To simply match this, use the following regex.

var matches = _videoUrl.match(/watch\?v=([a-zA-Z0-9\-_]+)/);
if (matches)
{
    alert('valid');
}

Naturally, the regex could be expanded to include the entire youtube url, but if all you need is the ID, this is the most surefire way I've found.

like image 32
Soviut Avatar answered Sep 27 '22 22:09

Soviut


You may try using a regular expression:

var url = 'youtube.com/watch?v=FhnMNwiGg5M';
var isyouTubeUrl = /((http|https):\/\/)?(www\.)?(youtube\.com)(\/)?([a-zA-Z0-9\-\.]+)\/?/.test(url);
like image 30
Darin Dimitrov Avatar answered Sep 27 '22 21:09

Darin Dimitrov