Moorad / youtube-video-downloader

199 stars 138 forks source link

Checking for invalid URLs #11

Closed Moorad closed 4 years ago

Moorad commented 5 years ago

The app currently does not check for invalid URLs and often will result in errors if an invalid URL is inserted. currently there is no indication for the user to know that the URL is invalid and this bug need to be fixed.

IgnacioVeiga commented 4 years ago

In script.js I tried the "validYoutubeURL" function and it works but only validates if the URL has the correct syntax. I hope it helps you, i am new to js.

Btn.addEventListener('click', () => {
    if (!URLinput.value) {
        alert('Enter YouTube URL');
    } else if (validYoutubeURL(URLinput.value.trim())) {
        if (select.value == 'mp3') {
            redirectMp3(URLinput.value);
        } else if (select.value == 'mp4') {
            redirectMp4(URLinput.value);
        }
    }
});

function validYoutubeURL(str) {
    if (!str) {
        return false;
    } else {
        var exp = new RegExp(/(youtu\.be\/|youtube\.com\/watch\?v=)([a-zA-Z0-9\-_]+)/);
        return exp.test(str);
    }
}
Moorad commented 4 years ago

I haven't tested this yet but it seems to be correct. One more thing though, if the user inputs in an invalid URL and clicks the button, there is no way for the user to know that the URL is invalid.

You might want to add an else statement that would alert() maybe? or do some kind of an error box or change the input box border to red? you can do whatever you want but alert() might be the easiest

Moorad commented 4 years ago

does that make sense?

JamesDeKat commented 4 years ago

Well this I think is the most clean option for checking a valid link

Edit: I apologise for my horrible site it's just for me and a couple of friends to use

Moorad commented 4 years ago

I see, the implementation is indeed correct but it still does not alert the user that the URL is incorrect.

Moorad commented 4 years ago

I will give this thread a couple of weeks and if I don't really get any response I'm happy to do it myself.