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.
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.
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+)?$/
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.
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;
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With