-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.js
134 lines (107 loc) · 3.55 KB
/
signup.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const form = document.getElementById('form');
const username = document.getElementById('username');
const email = document.getElementById('email');
const password = document.getElementById('password');
const password2 = document.getElementById('password2');
let dataArr = JSON.parse(localStorage.getItem('acc-data')) || [];
form.addEventListener('submit', e => {
e.preventDefault();
let userName1 = username.value;
let email1 = email.value;
let password1 = password.value;
let password2val = password2.value;
const errors = validateInputs();
if (errors.length > 0) {
errors.forEach(error => {
const inputField = error.input;
const errorMessage = error.message;
setError(inputField, errorMessage);
});
return;
}
const emailExists = dataArr.some(data => data.email === email1);
if (emailExists) {
setError(email, 'Email address already exists');
return;
}
var dataObj = {
userName: userName1,
email: email1,
password1: password1,
password2: password2val
};
dataArr.push(dataObj);
localStorage.setItem('acc-data', JSON.stringify(dataArr));
clearErrors();
clearFields();
showSuccessPopup();
});
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 usernameValue = username.value.trim();
const emailValue = email.value.trim();
const passwordValue = password.value.trim();
const password2Value = password2.value.trim();
if (usernameValue === '') {
errors.push({ input: username, message: 'Username is required' });
} else {
setSuccess(username);
}
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 if (passwordValue.length < 8) {
errors.push({ input: password, message: 'Password must be at least 8 characters' });
} else {
setSuccess(password);
}
if (password2Value === '') {
errors.push({ input: password2, message: 'Please confirm your password' });
} else if (password2Value !== passwordValue) {
errors.push({ input: password2, message: "Passwords don't match" });
} else {
setSuccess(password2);
}
return errors;
};
const clearErrors = () => {
const errorElements = document.querySelectorAll('.error');
errorElements.forEach(element => {
element.innerText = '';
const inputControl = element.parentElement;
inputControl.classList.remove('error');
});
};
const clearFields = () => {
username.value = '';
email.value = '';
password.value = '';
password2.value = '';
};
const showSuccessPopup = () => {
alert('Account created successfully!');
};