Regular expression to validate embed youtube url format

Hi,

I want to validate format of embed youtube url for input field.

Allowed format => https://www.youtube.com/embed/somecode

Not allowed format => https://www.youtube.com/watch?v=somecode

below regular expression not validating allowed format

^http://(?:www.)?youtube.com/embed/[A-z0-9]

public static function isValidYoutubeUrl($url)
    {
        return preg_match('/^http:\/\/(?:www\.)?youtube.com\/embed\/[A-z0-9]',$url);
    }

Any idea?

-Thanks

First of all your regexp tries to match to HTTP url and the provided URL is HTTPS. Also you might wanna add + in the end so it will match one or more characters after embed instead of one character.

This do it for you?
^https:\/\/(?:www\.)?youtube.com\/embed\/[A-z0-9]+

If you wanna match both https and http you could use:
^(https|http):\/\/(?:www\.)?youtube.com\/embed\/[A-z0-9]+

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.