-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (65 loc) · 2.44 KB
/
index.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
// Hamburger Menu
const dropDown = document.getElementById('icon-1');
const closeUp = document.getElementById('close');
const items = document.querySelector('.img-1-nav');
const closePort = document.querySelector('.refer-1');
const closeAbout = document.querySelector('.refer-2');
const closeCont = document.querySelector('.refer-3');
const opening = () => { items.style.visibility = 'visible'; };
const closing = () => { items.style.visibility = 'hidden'; };
dropDown.addEventListener('click', opening);
closeUp.addEventListener('click', closing);
closePort.addEventListener('click', closing);
closeAbout.addEventListener('click', closing);
closeCont.addEventListener('click', closing);
// Form Validation
const nameError = document.querySelector('#name-err');
const emailError = document.querySelector('#email-err');
const textError = document.querySelector('#text-err');
const submitError = document.querySelector('#submit-err');
function validateName() {
const name = document.querySelector('#name-id').value;
if (name.length === 0) {
nameError.innerHTML = 'Name is required';
return false;
}
if (!name.match(/^[A-Za-z]*\s{1}[A-Za-z]*$/)) {
nameError.innerHTML = 'Wrie full name';
return false;
}
nameError.innerHTML = '<i class="fa-regular fa-circle-check" style="color: #3bc456;"></i>';
return true;
}
function validateEmail() {
const email = document.querySelector('#email-id').value;
if (email.length === 0) {
emailError.innerHTML = 'Email is required';
return false;
}
if (!email.match(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/)) {
emailError.innerHTML = 'Invalid email';
return false;
}
emailError.innerHTML = '<i class="fa-regular fa-circle-check" style="color: #3bc456;"></i>';
return true;
}
function validateText() {
const text = document.querySelector('#text-area').value;
const length = 30;
const required = length - text.length;
if (required > 0) {
textError.innerHTML = `${required}more characters left`;
return false;
}
textError.innerHTML = '<i class="fa-regular fa-circle-check" style="color: #3bc456;"></i>';
return true;
}
function validate() { // eslint-disable-line no-unused-vars
if (!validateName() || !validateEmail() || !validateText()) {
submitError.style.display = 'block';
submitError.innerHTML = 'Please fix error!';
setTimeout(() => { submitError.style.display = 'none'; }, 3000);
return false;
}
return true;
}