> ## Content Index
> Fetch the complete content index at: https://irishdotnet.dev/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to validate a YouTube url using JavaScript
- URL: https://irishdotnet.dev/how-to-validate-a-youtube-url-using-javascript/
- Published: 2018-08-02T08:50:00.000Z
- Updated: 2024-09-16T21:27:20.000Z
- Author: Richard Reddy
- Tags: 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](http://www.youtube.com/?ref=irishdotnet.dev) 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.

``````
function validateYouTubeUrl(urlToParse){
                if (urlToParse) {
                    var regExp = /^(?:https?:\/\/)?(?:m\.|www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;
                    if (url.match(regExp)) {
                        return true;
                    }
                }
                return false;
            }
```

## Enjoyed what you just read?

Help fuel my next post ☕

[Make my day and treat me to a coffee!](https://irishdotnet.dev/#/portal/support)