JSFiddle - React, Tailwind, and code Playground

HTML

<input type="text" id="txt" placeholder="type you tube url" />

<input type="button" onclick="check(this)" value="Validate URL" />
<p id="aa"></p>

JavaScript

function matchYoutubeUrl(url) {
    var p = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;
    var matches = url.match(p);
    if(matches){
        return matches[1];
    }
    return false;
}


function check(sender){
    var url = $('#txt').val();
    var id = matchYoutubeUrl(url);
    if(id!=false){
        alert(id);
    }else{
        alert('Incorrect URL');
    }
}