Search code examples
javascriptjqueryhtmlrestrict

How do i only input link from one website?


I have another question. How do I restrict the input form to only accept links from a certain website for example Youtube should have this link included https://www.youtube.com/--- and by clicking the add button you would only be allowing that type of link in the list. I have looked everywhere but could not find anything.

<script>
    var input = document.getElementById('geturl');
    var button = document.getElementById('addlist');



    var addurl = function() {
        var url = input.value;
        var li = document.createElement('li');
        li.innerHTML = url;
        song = document.getElementById('songurl');



        if (url.length == 0) {
            alert('Please input data in the field.');
            console.log('No text in input.');

        } else {
            song.appendChild(li);
            document.getElementById("geturl").value = "";
            console.log('Url added');
        }
    }

    button.onclick = addurl;

</script>

</-->
<body>
<header>
    <nav>
        <div class="vidplay-title">
            <div class="logo">
                <i class="fa fa-forward" style="font-size:24px"></i>
            </div>
            <div class="title">
                <h2>Music Share</h2>
            </div>  
        </div>
    </nav>
</header>   
<section id="content">
<form name="form">
    <div class="search">
    <input type="text" placeholder="paste url..." id="geturl">
    <input type="submit" value="Add" id="addlist" onclick="return empty()" name="url">

    </div>
    <article>
        <div class="container" id=#list>
            <h5>Share your youtube url list and enjoy.</h5>
            <div class="scroll list-item-group">
                <div class="song-list">
                    <ol class="song" id="songurl">
                </div>
            </div>

            <button class="btn btn-primary">Share</button>
    </article>




    </div>
    </form>


</section>
<section>
    <footer>
        <div class="footer">
            <p>@Copyright</p>
        </div>

    </footer>
</section>

Solution

  • Use e.g. startsWith() (or a regular expression):

    if (url.length == 0) {
       alert('Please input data in the field.');
       return;
    }
    if (!url.startsWith("https://www.youtube.com/")) {
       alert("Not a YouTube link.");
       return;
    }
    song.appendChild(li);
    document.getElementById("geturl").value = "";
    // etc...