-
Notifications
You must be signed in to change notification settings - Fork 0
/
signin.js
85 lines (70 loc) · 2.32 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
78
79
80
81
82
83
84
85
const form = document.getElementById('form');
const email = document.getElementById('email');
const password = document.getElementById('password');
let dataArr = JSON.parse(localStorage.getItem('acc-data')) || [];
form.addEventListener('submit', e => {
e.preventDefault();
const emailValue = email.value.trim();
const passwordValue = password.value.trim();
const errors = validateInputs();
if (errors.length > 0) {
errors.forEach(error => {
const inputField = error.input;
const errorMessage = error.message;
setError(inputField, errorMessage);
});
return;
}
var found = false;
dataArr.map(function(ele) {
if (ele.email === emailValue && ele.password1 === passwordValue) {
found = true;
return false;
}
});
if (found) {
alert('Sign in Successful');
window.location.href = "index.html";
email.value = '';
password.value = '';
} else {
setError(email, 'Incorrect email or password');
setError(password, 'Incorrect email or password');
}
});
const setError = (element, message) => {
const inputControl = element.parentElement;
const errorDisplay = inputControl.querySelector('.error');
errorDisplay.innerText = message;
inputControl.classList.add('error');
inputControl.classList.remove('success');
};
const setSuccess = element => {
const inputControl = element.parentElement;
const errorDisplay = inputControl.querySelector('.error');
errorDisplay.innerText = '';
inputControl.classList.add('success');
inputControl.classList.remove('error');
};
const isValidEmail = email => {
const 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,}))$/;
return re.test(String(email).toLowerCase());
};
const validateInputs = () => {
const errors = [];
const emailValue = email.value.trim();
const passwordValue = password.value.trim();
if (emailValue === '') {
errors.push({ input: email, message: 'Email is required' });
} else if (!isValidEmail(emailValue)) {
errors.push({ input: email, message: 'Provide a valid email address' });
} else {
setSuccess(email);
}
if (passwordValue === '') {
errors.push({ input: password, message: 'Password is required' });
} else {
setSuccess(password);
}
return errors;
};