Newer
Older
music-loader / app.js
@ubuntu ubuntu on 23 May 1 KB init
function validateURL(url) {
    const pattern = new RegExp('^(https?:\\/\\/)?' + // проверка на протокол
        '((([a-zA-Z\\d]([a-zA-Z\\d-]*[a-zA-Z\\d])*)\\.)+[a-zA-Z]{2,}|' + // доменные имена
        '((\\d{1,3}\\.){3}\\d{1,3}))' + // или IP (v4) адреса
        '(\\:\\d+)?(\\/[-a-zA-Z\\d%_.~+]*)*' + // порт и путь
        '(\\?[;&a-zA-Z\\d%_.~+=-]*)?' + // параметры запроса
        '(\\#[-a-zA-Z\\d_]*)?$', 'i'); // якорь (фрагмент)
    
    return pattern.test(url);
}

document.addEventListener("DOMContentLoaded", e => {
	console.log("App start");

	const form = document.querySelector(".main-form");
	const startBtn = document.querySelector("[data-start-download]");
	const urlToPlaylistInp = document.querySelector("[name='url-to-playlist']");
	const playlistNameInp = document.querySelector("[name='playlist-name']");

	form.resetValidationClasses = () => form.querySelectorAll(".is-invalid").forEach(i => i.classList.remove("is-invalid"));

	form.querySelectorAll("input").forEach(i => i.addEventListener("input", e => {
		e.currentTarget.classList.remove("is-invalid");
	}));
	
	startBtn.addEventListener("click", e => {
		if(startBtn.classList.contains("disabled")) {
			return;
		}

		form.resetValidationClasses();
	
		let plUrl = urlToPlaylistInp.value;
		let plName = playlistNameInp.value;
		let dataIsValid = true;

		if(!plUrl.length || !validateURL(plUrl)) {
			urlToPlaylistInp.classList.add("is-invalid");
			dataIsValid = false;
		}

		if(plName.length < 4) {
			playlistNameInp.classList.add("is-invalid");
			dataIsValid = false;
		}

		if(!dataIsValid) {
			return;
		}

		
	});
});