Validating YouTube video URLs with jQuery and RegEx

Hey everyone! I’m trying to figure out how to validate YouTube video URLs using jQuery and regular expressions. I’ve looked at some other posts but couldn’t find exactly what I need.

I want to validate URLs like these:

youtube.com/watch?v=abc123
www.youtube.com/watch?v=xyz789&feature=popular
youtube.com/watch?v=def456&feature=related

I tried this code:

function checkYouTubeURL(url) {
  const pattern = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/watch\?v=[\w-]{11}(&\S*)?$/;
  return pattern.test(url);
}

$('#checkButton').click(function() {
  const inputURL = $('#urlInput').val();
  if (checkYouTubeURL(inputURL)) {
    console.log('Valid YouTube URL');
  } else {
    console.log('Invalid YouTube URL');
  }
});

It works for most cases, but it’s not perfect. Any ideas on how to improve this regex or approach? Thanks!

hey mate, ur regex looks decent but it misses some cases. try this one:

/^(?:https?://)?(?:www.|m.)?(?:youtube.com|youtu.be)/(?:watch?v=|embed/|v/|shorts/)?(?:[\w-]{11})(?:[&?].*)?$/

it covers more stuff like mobile links n shortened urls. but tbh, regex alone aint perfect for youtube validation. maybe combine it with an API check for foolproof results?

Your approach is on the right track, but there’s room for improvement. I’d suggest expanding your regex to handle more URL variations. Here’s a pattern I’ve found effective:

/^(?:https?://)?(?:www.|m.)?(?:youtube.com|youtu.be)/(?:watch?v=|embed/|v/|shorts/|playlist?list=)?([\w-]{11})(?:[?&].*)?$/

This covers standard watch URLs, mobile versions, shortened links, embeds, and even playlist URLs. It’s more comprehensive and future-proof.

Remember, though, that regex alone isn’t foolproof. For bulletproof validation, consider implementing a two-step process: first, use regex for initial filtering, then make an API call to YouTube’s data API to confirm the video exists. This approach has served me well in production environments, ensuring robust URL validation while minimizing false positives.

As someone who’s worked extensively with YouTube API and URL validation, I can share a few insights that might help improve your approach.

Your current regex is a good start, but it doesn’t account for all possible YouTube URL formats. For instance, it misses shortened youtu.be links and embed URLs. Here’s an enhanced regex I’ve used in production:

/^(?:https?://)?(?:www.)?(?:youtube.com|youtu.be)/(?:watch?v=|embed/|v/|shorts/)?(?:[\w-]{11})(?:[?&].+)?$/

This pattern covers more bases, including embed links and the newer ‘shorts’ format.

One thing to keep in mind: regex alone isn’t foolproof for YouTube URL validation. YouTube’s URL structure can change, and there are edge cases that regex might miss. For robust validation, I’d recommend using a combination of regex and API calls to YouTube’s oEmbed endpoint.

Also, consider using async/await for smoother handling of API responses if you decide to incorporate API checks. It’s made my code much cleaner and easier to maintain in similar projects.