-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
293 lines (240 loc) · 9.21 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Sign-up Form
const submit = document.getElementById('submitBtn');
submit.disabled = true;
// get all input elements
const inputs = document.querySelectorAll('input');
inputs.forEach(input => {
input.addEventListener('keyup', (a) => {
enableSubmit();
});
});
//if all input is valid, enable submit button
function enableSubmit() {
if (firstName.validity.valid && lastName.validity.valid && phone.validity.valid && pass.validity.valid && email.validity.valid && confirmPass.validity.valid) {
submit.disabled = false;
} else {
submit.disabled = true;
}
}
//for email validation
// There are many ways to pick a DOM node; here we get the form itself and the email
// input box, as well as the span element into which we will place the error message.
const form = document.getElementsByTagName('form')[0];
const email = document.getElementById('mail');
const error = document.getElementById('error');
// const emailError = document.querySelector('#mail + span.error');
error.innerText = email.validationMessage;
error.classList.add('invalid');
error.classList.remove('valid');
email.addEventListener('input', function (event) {
// Each time the user types something, we check if the
// form fields are valid.
// If the email field is empty, show the error message
if (email.validity.valid) {
// In case there is an error message visible, if the field
// is valid, we remove the error message.
// emailError.innerText = email.validationMessage; // Reset the content of the message
// emailError.className = 'error'; // Reset the visual state of the message
error.innerText = "Valid E-mail Address";
error.classList.remove('invalid');
error.classList.add('valid');
} else {
// If there is still an error, show the correct error
showError();
}
});
form.addEventListener('submit', function (event) {
// if the form contains valid data, we let it submit
event.preventDefault();
if (!email.validity.valid) {
submit.disabled = true;
// If it isn't, we display an appropriate error message
showError();
// Then we prevent the form from being sent by canceling the event
event.preventDefault();
}
});
function showError() {
if (email.validity.valueMissing) {
// If the field is empty
// display the following error message.
error.textContent = 'You need to enter an e-mail address.';
} else if (email.validity.typeMismatch) {
// If the field doesn't contain an email address
// display the following error message.
error.textContent = 'Entered value needs to be an e-mail address.';
} else if (email.validity.tooShort) {
// If the data is too short
// display the following error message.
error.textContent = `Email should be at least ${email.minLength} characters; you entered ${email.value.length}.`;
}
// Set the styling appropriately
// error.className = 'error active';
error.classList.add('invalid');
error.classList.remove('valid');
}
// for password validation
const pass = document.getElementById("user_password");
const userPasswordVal = document.getElementById("userPasswordVal");
userPasswordVal.innerText = "Please Enter a Valid Password";
userPasswordVal.classList.add('invalid');
userPasswordVal.classList.remove('valid');
const myInput = document.getElementById("user_password");
const letter = document.getElementById("letter");
const capital = document.getElementById("capital");
const number = document.getElementById("number");
const length = document.getElementById("length");
// When the user clicks on the password field, show the message box
myInput.onfocus = function () {
document.getElementById("message").style.display = "block";
}
// When the user clicks outside of the password field, hide the message box
myInput.onblur = function () {
document.getElementById("message").style.display = "none";
}
// When the user starts to type something inside the password field
myInput.onkeyup = function () {
// Validate lowercase letters
let lowerCaseLetters = /[a-z]/g;
if (myInput.value.match(lowerCaseLetters)) {
letter.classList.remove("invalid");
letter.classList.add("valid");
} else {
letter.classList.remove("valid");
letter.classList.add("invalid");
}
// Validate capital letters
let upperCaseLetters = /[A-Z]/g;
if (myInput.value.match(upperCaseLetters)) {
capital.classList.remove("invalid");
capital.classList.add("valid");
} else {
capital.classList.remove("valid");
capital.classList.add("invalid");
}
// Validate numbers
let numbers = /[0-9]/g;
if (myInput.value.match(numbers)) {
number.classList.remove("invalid");
number.classList.add("valid");
} else {
number.classList.remove("valid");
number.classList.add("invalid");
}
// Validate length
if (myInput.value.length >= 8) {
length.classList.remove("invalid");
length.classList.add("valid");
} else {
length.classList.remove("valid");
length.classList.add("invalid");
}
if(pass.validity.valid){
passIsValid();
}
}
function passIsValid() {
userPasswordVal.innerText = "Valid Password";
userPasswordVal.classList.remove('invalid');
userPasswordVal.classList.add('valid');
userPasswordVal.style.color = "green";
}
// for password confirmation validation
const divCheckPassword = document.getElementById('divCheckPassword');
const confirmPass = document.getElementById('user_password_confirm');
const confirmPassVal = document.getElementById('confirmPassVal');
const userPasswordConfirmVal = document.getElementById('userPasswordConfirmVal');
userPasswordConfirmVal.innerText = "Please Confirm Password";
userPasswordConfirmVal.classList.add('invalid');
userPasswordConfirmVal.classList.remove('valid');
confirmPass.addEventListener('keyup', (e) => {
let password = pass.value;
let confirmPassword = confirmPass.value;
userPasswordConfirmVal.style.display = "none";
if (password != confirmPassword) {
divCheckPassword.innerHTML = "Passwords do not match!";
divCheckPassword.classList.remove("valid");
divCheckPassword.classList.add("invalid");
submit.disabled = true;
} else {
divCheckPassword.innerHTML = "Password Confirmed";
divCheckPassword.classList.remove("invalid");
divCheckPassword.classList.add("valid");
}
});
//for first name, last name and phone validation
const firstName = document.getElementById('first_name');
const firstNameVal = document.getElementById('firstNameVal');
const lastName = document.getElementById('last_name');
const lastNameVal = document.getElementById('lastNameVal');
const phone = document.getElementById('phone_number');
const phoneNumberVal = document.getElementById('phoneNumberVal');
function validateForm() {
firstNameVal.innerText = firstName.validationMessage;
firstNameVal.classList.remove("valid");
firstNameVal.classList.add("invalid");
lastNameVal.innerText = lastName.validationMessage;
lastNameVal.classList.remove("valid");
lastNameVal.classList.add("invalid");
phoneNumberVal.innerText = phone.validationMessage;
phoneNumberVal.classList.remove("valid");
phoneNumberVal.classList.add("invalid");
firstName.addEventListener('input', function (event) {
// Each time the user types something, we check if the
// form fields are valid.
if (firstName.validity.valid) {
// In case there is an error message visible, if the field
// is valid, we remove the error message.
firstNameVal.innerHTML = 'Valid First Name'; // Reset the content of the message
firstNameVal.classList.remove("invalid");
firstNameVal.classList.add("valid");
firstNameVal.style.color = 'green';
} else {
// If there is still an error, show the correct error
firstNameVal.innerHTML = firstName.validationMessage;
firstNameVal.classList.remove("valid");
firstNameVal.classList.add("invalid");
firstNameVal.style.color = 'rgb(185, 1, 1)';
submit.disabled = true;
}
});
lastName.addEventListener('input', function (event) {
// Each time the user types something, we check if the
// form fields are valid.
if (lastName.validity.valid) {
// In case there is an error message visible, if the field
// is valid, we remove the error message.
lastNameVal.innerHTML = 'Valid Last Name'; // Reset the content of the message
lastNameVal.classList.remove("invalid");
lastNameVal.classList.add("valid");
lastNameVal.style.color = 'green';
} else {
// If there is still an error, show the correct error
lastNameVal.innerHTML = lastName.validationMessage;
lastNameVal.classList.remove("valid");
lastNameVal.classList.add("invalid");
lastNameVal.style.color = 'rgb(185, 1, 1)';
submit.disabled = true;
}
});
phone.addEventListener('input', function (event) {
// Each time the user types something, we check if the
// form fields are valid.
if (phone.validity.valid) {
// In case there is an error message visible, if the field
// is valid, we remove the error message.
phoneNumberVal.innerHTML = 'Valid Phone Number'; // Reset the content of the message
phoneNumberVal.classList.remove("invalid");
phoneNumberVal.classList.add("valid");
phoneNumberVal.style.color = 'green';
} else {
// If there is still an error, show the correct error
phoneNumberVal.innerHTML = phone.validationMessage;
phoneNumberVal.classList.remove("valid");
phoneNumberVal.classList.add("invalid");
phoneNumberVal.style.color = 'rgb(185, 1, 1)';
submit.disabled = true;
}
});
}
validateForm();