I'm having a difficult time trying to add a validator method to check not only if a URL is allowed (from YouTube or Vimeo using a regex string), but also if the video actually exists on the sites and isn't a 404.
So far I have my URL validator matching the URL's correctly and was trying to test the remote validation using oEmbed:
var vidRegExp = /https?:\/\/(?:[0-9A-Z\-]+\.)?(vimeo\.com\/(clip\:)?(\d+).*|player\.vimeo\.com\/video\/?(\d+).*|(?:youtu\.be\/|youtube\.com\S*[^\w\-\s])([\w\-]{11})(?=[^\w\-]|$)(?![?=&+%\w]*(?:['"][^<>]*>|<\/a>))[?=&+%\w\-]*)/i;
jQuery.validator.addMethod("checkVideoURL", (function(val, elem) {
if (val.length === 0) {
return true;
}
var match = val.match(vidRegExp);
var settings = $('form').validate().settings;
if (match) {
$.extend(true, settings, {
rules: {
item_provider_url: {
remote: {
url: "http://vimeo.com/api/oembed.json",
dataType: "jsonp",
data: {
format: "jsonp",
url: function() {
return $("#item_provider_url").val;
}
}
}
}
},
messages: {
item_provider_url: {
remote: "No video exists at this URL"
}
}
});
} else {
$.validator.messages.checkVideoURL = "The URL you put in is not a valid YouTube or Vimeo URL. Please check your link.";
return false;
}
}), "Invalid URL.");
I've been pouring over posts on StackOverflow searching for the correct way to do this and can't seem to find anything that helps. What's not working above is the remote validation part. (example 404/dead link: http://vimeo.com/54634709)
Also, since obviously YouTube and Vimeo have different URL's to check against, I need to know how to check the matched URL and return a different remote URL for each different site. Another big plus would be to extend the check to see if embedding is allowed with the video.
The regex string above works with all these URL's:
http://vimeo.com/50406912
https://vimeo.com/50406912
http://www.vimeo.com/50406912
https://www.vimeo.com/50406912
http://soundcloud.com/forss/flickermood
http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
http://www.youtube.com/embed/0zM3nApSvMg?rel=0
http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
http://www.youtube.com/watch?v=0zM3nApSvMg
http://youtu.be/0zM3nApSvMg
http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s
http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o
https://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s
https://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o
http://vimeo.com/36888803
http://vimeo.com/36888803?title=0
http://player.vimeo.com/video/36888803
http://player.vimeo.com/video/36888803?title=0&byline=0&portrait=0
http://youtu.be/i4fjHzCXg6c
http://youtu.be/i4fjHzCXg6c?t=9s
http://www.youtube.com/watch?v=i4fjHzCXg6c
https://www.youtube.com/watch?v=i4fjHzCXg6c&feature=g-logo&t=28s
Any and all help would be greatly appreciated!