How to validate youtube video ids?

Tom picture Tom · Apr 30, 2010 · Viewed 29.1k times · Source

I want to validate youtube video ids sbumitted in the URL to one of my sites before accessing the Youtube API, but I don't know what the allowed characters are in such an id. I see people on the net guessing it can contain numbers and characters, but I haven't yet seen an official specification of these video ids.

Is there one?

Answer

roman m picture roman m · Nov 3, 2010

See this thread for official info.

you can hit this: http://gdata.youtube.com/feeds/api/videos/VIDEO_ID (Page now returns: "No longer available".)

and determine if the video is valid based on response

There's no way you can check the validity of the ID with RegEx, since not all alpha-numeric values are valid ID's.

p.s. i'm pretty sure i saw "dashes" in video ID's

p.p.s. "underscore" is a valid character also: http://www.youtube.com/watch?v=nrGk0AuFd_9

[a-zA-Z0-9_-]{11} is the regex (source), but there's no guarantee that the video will be there even if regex is valid