-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
93 lines (79 loc) · 2.53 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const btn = document.getElementById("menu-btn")
const menu = document.getElementById("menu")
const input = document.getElementById("link-input");
const linkForm = document.getElementById("link-form");
const errMsg = document.getElementById("err-msg");
btn.addEventListener("click", navToggle)
linkForm.addEventListener("submit", formSubmit);
// Toggle Moblie Menu
function navToggle() {
btn.classList.toggle("open");
menu.classList.toggle("flex");
menu.classList.toggle("hidden");
}
function validURL(str) {
var pattern = new RegExp(
"^(https?:\\/\\/)?" + // protocol
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" +
"((\\d{1,3}\\.){3}\\d{1,3}))" +
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" +
"(\\?[;&a-z\\d%_.~+=-]*)?" +
"(\\#[-a-z\\d_]*)?$",
"i"
);
return !!pattern.test(str);
}
function formSubmit(e) {
e.preventDefault();
if (input.value === "") {
errMsg.innerHTML = "Please enter something";
input.classList.add("border-red");
} else if (!validURL(input.value)) {
errMsg.innerHTML = "Please enter a valid URL";
input.classList.add("border-red");
} else {
errMsg.innerHTML = "";
input.classList.remove("border-red");
alert("Success");
}
}
// const btn = document.getElementById('menu-btn')
// const menu = document.getElementById('menu')
// const input = document.getElementById('link-input')
// const linkForm = document.getElementById('link-form')
// const errMsg = document.getElementById('err-msg')
// btn.addEventListener('click', navToggle)
// linkForm.addEventListener('submit', formSubmit)
// // Toggle Mobile Menu
// function navToggle() {
// btn.classList.toggle('open')
// menu.classList.toggle('flex')
// menu.classList.toggle('hidden')
// }
// // Validate a URL
// function validURL(str) {
// var pattern = new RegExp(
// '^(https?:\\/\\/)?' + // protocol
// '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' +
// '((\\d{1,3}\\.){3}\\d{1,3}))' +
// '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' +
// '(\\?[;&a-z\\d%_.~+=-]*)?' +
// '(\\#[-a-z\\d_]*)?$',
// 'i'
// )
// return !!pattern.test(str)
// }
// function formSubmit(e) {
// e.preventDefault()
// if (input.value === '') {
// errMsg.innerHTML = 'Please enter something'
// input.classList.add('border-red')
// } else if (!validURL(input.value)) {
// errMsg.innerHTML = 'Please enter a valid URL'
// input.classList.add('border-red')
// } else {
// errMsg.innerHTML = ''
// input.classList.remove('border-red')
// alert('Success')
// }
// }