-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.js
67 lines (64 loc) · 2.41 KB
/
form.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
//form
//regex in this taken from : https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript
function validateEmail(email) {
let re = /^(([^<>()[\]\\.,;:\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,}))$/;
if (re.test(email)) {
document.body.style.backgroundColor = 'rgb(165, 241, 165)';
return true;
}
else {
document.body.style.backgroundColor = 'rgb(248, 126, 126)';
return false;
}
}
function validateUser(username) {
if (username.length < 6) {
document.body.style.backgroundColor = 'rgb(248, 126, 126)';
return false;
}
else {
document.body.style.backgroundColor = 'rgb(165, 241, 165)';
return true;
}
}
const downArrow = document.querySelectorAll('.fa-arrow-down');
downArrow.forEach(arrow => {
arrow.addEventListener('click', () => {
const field = arrow.previousElementSibling;
const parent = arrow.parentElement;
const nextElement = parent.nextElementSibling;
function correctInput() {
parent.classList.add('inactive');
parent.classList.remove('active');
nextElement.classList.add('active');
nextElement.classList.remove('inactive');
}
if (field.id === 'username' && validateUser(field.value)) {
//username
correctInput();
}
else if (field.id === 'email' && validateEmail(field.value)) {
//email
correctInput();
}
else if (field.id === 'password' && validateUser(field.value)) {
//password
correctInput();
setTimeout(() => {
window.location.replace('index.html');
}, 2000);
}
else {
//rotate the parent as at least one stage the parent didn't validate correctly
if (parent.style.animation) {
parent.style.animation = ''
}
else {
parent.style.animation = 'rotateField 0.5s ease'
}
}
parent.addEventListener('animationend', () => {
parent.style.animation = ''
}); //animation end is needed to track if animation is still on ,if its not on then only the next iteration will go .
});
});