0

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>
coder4ever
  • 37
  • 6
  • This has been answered in the following Stack question with a Javascript implementation - https://stackoverflow.com/a/3809435/825475 – Joe Z Feb 26 '20 at 12:55

4 Answers4

2

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...
AKX
  • 152,115
  • 15
  • 115
  • 172
1

You can use regex to get a more general validator since all url dont look like this https://youtube.com/

var pattern = /^(?:https?:\/\/)?(?:m\.|www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;
if (!url.match(pattern)) {
  // Invalid link
}
PEPEGA
  • 2,214
  • 20
  • 37
0

Check the input String if it starts with your desired URL, e.g. https://youtube.com/

Just like in your example code, with if(url.length == 0). But instead of length you can use a Regular Expression to check your input.

var pattern = /* your regular expression */;
if (!url.match(pattern)) {
    alert("wrong url");
}
Fred
  • 220
  • 2
  • 8
-1

The simple answer is to only accept partial URLS or more particularly the path bit after https://youtube.com instead of the full URL

vogomatix
  • 4,856
  • 2
  • 23
  • 46