Code Snippet

  • How to validate a YouTube url using JavaScript

    validate a YouTube url using JavaScript

    Here is a little code snippet to help you validate a value to ensure it's a YouTube url using JavaScript.

    This regex here will ensure that the url supplied matches the criteria of a valid youtube url:

    • This is that it is from www.youtube.com or youtu.be
    • That it contains either a 'v=' querystring or has the id as part of the url supplied
    • It also makes sure that the id is 11 characters long.

  • How to extract a YouTube video id from a url

    how to get the video id from a youtube url

    If you're allowing customers to save YouTube videos on your site and you'd like to extract out the video id when storing into your database you could try using either of these options below.

    The thing to remember is that the YouTube url can be either:

    • Youtube.com
    • Youtu.be

    and the Video Id can be appended as a querystring parameter or as the last segment of the url:

    • YouTubeUrl?v=videoId
    • YouTubeUrl/videoId

Get In Touch

Follow me online at TwitterFacebook or Flickr.

Latest Tweets