How can I check if a given URL points to a valid YouTube video?
Asked
Active
Viewed 3,347 times
1
-
check this link , this works :http://stackoverflow.com/questions/28735459/how-to-validate-youtube-url-in-client-side-in-text-box – srikanth chitturi Jan 10 '17 at 11:33
2 Answers
0
Match it with a regular expression.
if($parsed[0].match(/youtube\.com/) {
// affirmative
} else {
// negative
}
I'm calling match()
on the first element of $parsed
, but your "domain" portion could very well be the 3rd element if your URL string started with "http://".
There's probably a more rock-solid regex you could use there, but that'll evaluate as true if string contains the youtube.com
domain.

Edd Morgan
- 2,873
- 17
- 22
0
The below function will return true or false on passing url:
function matchYoutubeUrl(url){
var p = /^(?:https?:\/\/)?(?:www\.)? (?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;
return (url.match(p)) ? true : false ;
}

atul singh
- 664
- 5
- 11
-
i know this is old but it doesn't work for me, it returns false even if the URL is true – Fanckush Jan 25 '15 at 14:41