-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
129 lines (108 loc) · 3.96 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// validation function
const validate = (result) => {
const response = []
const keys = Object.keys(result).filter(key => key !== "content")
// return true if the whole inputs are validated otherwise false
keys.forEach(key => {
let el = document.getElementById(key)
const value = result[key].value
if(value === "") {
if(el.classList.contains("is-valid")) {
el.classList.remove("is-valid")
}
el.classList.add("is-invalid")
el.nextElementSibling.textContent = `Enter a valid ${key}`
// false if value is empty
response[key] = false
} else {
if(el.classList.contains("is-invalid")) {
el.classList.remove("is-invalid")
}
switch (key) {
case "name":
// true if name has at least three chars
response[key] = value.length >= 3 ? true : false
if(response) {
el.classList.add("is-valid")
el.nextElementSibling.textContent = ""
} else {
el.classList.add("is-invalid")
el.nextElementSibling.textContent = `${key} must have at least three characters`
}
break
case "email":
// true if email check is ok
const regex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
response[key] = regex.test(value) ? true : false
if(response) {
el.classList.add("is-valid")
el.nextElementSibling.textContent = ""
} else {
el.classList.add("is-invalid")
el.nextElementSibling.textContent = `Enter a valid ${key}`
}
break
default:
response[key] = true
break
}
}
})
for(const key in response) {
if(!response[key]) {
return false
}
}
return true
}
form.onsubmit = async (e) => {
e.preventDefault()
const form = e.target
let result = {
name: form.name,
email: form.email,
issue: form.issue,
content: form.content,
}
// print result
const body = document.getElementById("result")
body.innerHTML = `<div>
<div><strong>Name</strong> ${result.name.value}</div>
<div><strong>Email</strong> ${result.email.value}</div>
<div><strong>Request</strong> ${result.issue.value}</div>
<p>
<div><strong>Content of the contact request</strong></div>
<div>${result.content.value === "" ? "No content" : result.content.value.replace(/(<([^>]+)>)/gi, "")}</div>
</p>
</div>`
const myModal = new bootstrap.Modal(".modal")
const dialog = myModal._element
if(dialog.classList.contains("animate-out")) {
dialog.classList.remove("animate-out")
}
dialog.classList.add("animate-in")
const isValid = validate(result)
// show modal if every value in result is not empty except content otherwise validate form
if(isValid) {
myModal.show()
const closeBtn = document.getElementById("close")
closeBtn.addEventListener('click', e => {
e.preventDefault()
dialog.classList.remove("animate-in")
dialog.classList.add("animate-out")
dialog.onanimationend = () => {
myModal.hide()
}
})
}
}
document.addEventListener("DOMContentLoaded", () => {
const form = document.forms.form
Array.from(form).slice(0, 2).forEach(el => {
el.onchange = e => {
const obj = {}
obj[e.currentTarget.name] = e.currentTarget
validate(obj)
}
})
})