File size: 798 Bytes
5ec2e9b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /**
* Validates a YouTube video ID format
* YouTube video IDs are 11 characters long and contain alphanumeric characters, hyphens, and underscores
* Reference: https://webapps.stackexchange.com/questions/54443/format-for-id-of-youtube-video
*
* @param videoId - The video ID to validate
* @returns true if the video ID is valid, false otherwise
*/
export const validateVideoId = (videoId: string): boolean => {
// Handle null, undefined, or non-string values
if (!videoId || typeof videoId !== "string") {
return false;
}
// YouTube video IDs are exactly 11 characters
if (videoId.length !== 11) {
return false;
}
// Valid characters: A-Z, a-z, 0-9, -, _
const validPattern = /^[A-Za-z0-9_-]{11}$/;
return validPattern.test(videoId);
};
|