generated from imd1005-web-development-winter-2023/group-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signin.js
77 lines (64 loc) · 1.9 KB
/
signin.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
const form = document.querySelector('form');
const email = document.querySelector('.sign-in-email');
const password = document.querySelector('.sign-in-password');
let isValid = false;
form.addEventListener('submit', (event) => {
event.preventDefault();
validate();
if (isValid == true) {
location.replace("./main.html");
}
else if (isValid == false) {
return;
}
});
const showError = (element, message) => {
const inputControl = element.parentElement;
const errorDisplay = inputControl.querySelector('.error');
errorDisplay.innerText = message;
inputControl.classList.add('error');
inputControl.classList.remove('success');
};
const showSuccess = (element) => {
const inputControl = element.parentElement;
const errorDisplay = inputControl.querySelector('.error');
errorDisplay.innerText = '';
inputControl.classList.add('success');
inputControl.classList.remove('error');
};
const checkEmail = (emailVal) => {
var atSymbol = emailVal.indexOf('@');
if (atSymbol < 1) return false;
var dot = emailVal.lastIndexOf('.');
if (dot <= atSymbol + 2) return false;
if (dot === emailVal.length - 1) return false;
return true;
};
const validate = () => {
const emailVal = email.value.trim();
const passwordVal = password.value.trim();
if (emailVal === '') {
showError(email, 'Email is required')
isValid == false;
}
else if (!checkEmail(emailVal)) {
showError(email, 'Email is invalid')
isValid == false;
}
else {
showSuccess(email);
isValid == true;
}
if (passwordVal === '') {
showError(password, 'Password is required')
isValid == false;
}
else if (passwordVal.length <= 7) {
showError(password, 'Password must be min 8 characters')
isValid == false;
}
else {
showSuccess(password)
isValid == true;
}
};