-
Notifications
You must be signed in to change notification settings - Fork 18
/
script.js
196 lines (194 loc) · 5.44 KB
/
script.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Input fields
const firstName = document.getElementById('firstName');
const lastName = document.getElementById('lastName');
const password = document.getElementById('password');
const confirmPassword = document.getElementById('confirmPassword');
const email = document.getElementById('email');
// Form
const form = document.getElementById('myForm');
// Validation colors
const green = '#4CAF50';
const red = '#F44336';
// Handle form
form.addEventListener('submit', function(event) {
// Prevent default behaviour
event.preventDefault();
if (
validateFirstName() &&
validateLastName() &&
validatePassword() &&
validateConfirmPassword() &&
validateEmail()
) {
const name = firstName.value;
const container = document.querySelector('div.container');
const loader = document.createElement('div');
loader.className = 'progress';
const loadingBar = document.createElement('div');
loadingBar.className = 'indeterminate';
loader.appendChild(loadingBar);
container.appendChild(loader);
setTimeout(function() {
const loaderDiv = document.querySelector('div.progress');
const panel = document.createElement('div');
panel.className = 'card-panel green';
const text = document.createElement('span');
text.className = 'white-text';
text.appendChild(
document.createTextNode(
`Sign up successful, welcom to SocialApe ${name}`
)
);
panel.appendChild(text);
container.replaceChild(panel, loaderDiv);
}, 1000);
}
});
// Validators
function validateFirstName() {
// check if is empty
if (checkIfEmpty(firstName)) return;
// is if it has only letters
if (!checkIfOnlyLetters(firstName)) return;
return true;
}
function validateLastName() {
// check if is empty
if (checkIfEmpty(lastName)) return;
// is if it has only letters
if (!checkIfOnlyLetters(lastName)) return;
return true;
}
function validatePassword() {
// Empty check
if (checkIfEmpty(password)) return;
// Must of in certain length
if (!meetLength(password, 4, 100)) return;
// check password against our character set
// 1- a
// 2- a 1
// 3- A a 1
// 4- A a 1 @
// if (!containsCharacters(password, 4)) return;
return true;
}
function validateConfirmPassword() {
if (password.className !== 'valid') {
setInvalid(confirmPassword, 'Password must be valid');
return;
}
// If they match
if (password.value !== confirmPassword.value) {
setInvalid(confirmPassword, 'Passwords must match');
return;
} else {
setValid(confirmPassword);
}
return true;
}
function validateEmail() {
if (checkIfEmpty(email)) return;
if (!containsCharacters(email, 5)) return;
return true;
}
// Utility functions
function checkIfEmpty(field) {
if (isEmpty(field.value.trim())) {
// set field invalid
setInvalid(field, `${field.name} must not be empty`);
return true;
} else {
// set field valid
setValid(field);
return false;
}
}
function isEmpty(value) {
if (value === '') return true;
return false;
}
function setInvalid(field, message) {
field.className = 'invalid';
field.nextElementSibling.innerHTML = message;
field.nextElementSibling.style.color = red;
}
function setValid(field) {
field.className = 'valid';
field.nextElementSibling.innerHTML = '';
//field.nextElementSibling.style.color = green;
}
function checkIfOnlyLetters(field) {
if (/^[a-zA-Z ]+$/.test(field.value)) {
setValid(field);
return true;
} else {
setInvalid(field, `${field.name} must contain only letters`);
return false;
}
}
function meetLength(field, minLength, maxLength) {
if (field.value.length >= minLength && field.value.length < maxLength) {
setValid(field);
return true;
} else if (field.value.length < minLength) {
setInvalid(
field,
`${field.name} must be at least ${minLength} characters long`
);
return false;
} else {
setInvalid(
field,
`${field.name} must be shorter than ${maxLength} characters`
);
return false;
}
}
function containsCharacters(field, code) {
let regEx;
switch (code) {
case 1:
// letters
regEx = /(?=.*[a-zA-Z])/;
return matchWithRegEx(regEx, field, 'Must contain at least one letter');
case 2:
// letter and numbers
regEx = /(?=.*\d)(?=.*[a-zA-Z])/;
return matchWithRegEx(
regEx,
field,
'Must contain at least one letter and one number'
);
case 3:
// uppercase, lowercase and number
regEx = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/;
return matchWithRegEx(
regEx,
field,
'Must contain at least one uppercase, one lowercase letter and one number'
);
case 4:
// uppercase, lowercase, number and special char
regEx = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)/;
return matchWithRegEx(
regEx,
field,
'Must contain at least one uppercase, one lowercase letter, one number and one special character'
);
case 5:
// Email pattern
regEx = /^(([^<>()\[\]\\.,;:\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 matchWithRegEx(regEx, field, 'Must be a valid email address');
default:
return false;
}
}
function matchWithRegEx(regEx, field, message) {
if (field.value.match(regEx)) {
setValid(field);
return true;
} else {
setInvalid(field, message);
return false;
}
}