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
Assuming you want a Youtube video URL rather than any YouTube URL, you can do it using a regex:
var url = 'youtube.com/watch?v=FhnMNwiGg5M';
var matches = url.match(/^(https?:\/\/)?([^\/]*\.)?youtube\.com\/watch\?([^]*&)?v=\w+(&[^]*)?/i);
You can check if URL contains the word "http://www.youtube.com" using .indexOf()
var url = 'http://www.youtube.com/channel/UChh-akEbUM8_6ghGVnJd6cQ';
if (url.indexOf('http://www.youtube.com') > -1) {
alert( "true");
} else {
alert( "false");
}
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;
you Want to validate your URL:
Please pass the URL in this function then this will give you true OR false
See Function :
<script>
function isValidUrl(s) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(s);
}
isValidUrl(yourURL);
</script>
I will give some extra :
For both Youtube and Vimeo to checking those IDs are really vaild or not
var GoogleKey = 'XXXXXXXXXX';
function GetVideoById(id) {
var site = !isNaN(id) ? 'vimeo' : 'youtube';
var data = false;
if(site==='youtube') {
$.ajax({
async: false,
url: 'https://www.googleapis.com/youtube/v3/videos?id='+id+'&key='+GoogleKey+'&part=snippet',
success: function(r) {
if(r['items'].length) {
data = {'type':'youtube','data':r['items'][0]['snippet']};
}
}
});
} else {
$.ajax({
async: false,
url: 'http://vimeo.com/api/v2/video/'+id+'.json',
success: function(r) {
data = {'type':'vimeo','data':r[0]};
}
});
}
return data;
}
So Example runs :
if(GetVideoById('YykjpeuMNEk')) {
// youtube + data
}
if(GetVideoById('162334918')) {
// vimeo + data
}
if(GetVideoById('999999')) {
// nothing (because false)
}
if(GetVideoById('abcdefg')) {
// nothing (because false)
}
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.