-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
93 lines (76 loc) · 2.96 KB
/
Copy pathscript.js
File metadata and controls
93 lines (76 loc) · 2.96 KB
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
window.addEventListener('DOMContentLoaded', (event) => {
const btnOpenModal = document.querySelectorAll('.open-modal')
const [modalWindow, succssefullModal] = document.querySelectorAll('.modal-window')
const elemCloseModal = document.querySelectorAll('.close-modal')
const [btnSendData] = document.querySelectorAll('.send-data')
const openSuccssefullModal = () => {
succssefullModal.classList.add('active')
document.body.classList.add('active')
}
const closeSuccssefullModal = () => {
succssefullModal.classList.remove('active')
document.body.classList.remove('active')
}
const openModal = () => {
modalWindow.classList.add('active')
document.body.classList.add('active')
}
const closeModal = () => {
modalWindow.classList.remove('active')
document.body.classList.remove('active')
}
const validation = (name, email, message) => {
const validateNotEmpty = (dataItem) => {
return dataItem !== ''
}
const validateEmail = (email) => {
return email.match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)
}
const elements = [name, email, message]
const inValidElements = []
for(let i = 0; i < elements.length; i++) {
if(i == 1 && !validateEmail(elements[i].value)) {
inValidElements.push(elements[i])
continue
}
if(!validateNotEmpty(elements[i].value)) inValidElements.push(elements[i])
}
return {
resultValid: [name.value, email.value, message.value].every(validateNotEmpty) && validateEmail(email.value),
notValid: inValidElements
}
}
const sendData = (event) => {
event.preventDefault();
const [name] = document.querySelectorAll('.input-send-name')
const [email] = document.querySelectorAll('.input-send-email')
const [textArea] = document.querySelectorAll('.input-send-textarea')
const validator = validation(name, email, textArea)
const allInputs = [name, email, textArea]
allInputs.map((elem) => {
elem.classList.remove('error')
})
if(!validator.resultValid) {
validator.notValid.map((elem) => {
elem.classList.add('error')
})
} else {
fetch('https://httpbin.org/post', {
method: 'POST'
}).then(() => {
closeModal();
openSuccssefullModal();
});
}
}
btnSendData.addEventListener('click', sendData)
btnOpenModal.forEach((elem) => {
elem.addEventListener('click', openModal)
})
elemCloseModal.forEach((elem) => {
elem.addEventListener('click', () => {
closeModal()
closeSuccssefullModal()
})
})
});