-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
178 lines (154 loc) · 5.25 KB
/
script.js
File metadata and controls
178 lines (154 loc) · 5.25 KB
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
class FormValidator {
constructor(form, fields) {
this.form = form;
this.fields = fields;
}
initialize() {
this.validateOnEntry();
this.validateOnSubmit();
}
validateOnEntry() {
let self = this; //stores the value of the 'this' keyword on the constructor
this.fields.forEach(field => {
const input = document.querySelector(`#${field}`);
input.addEventListener("keyup", () => {
self.validateInput(input);
})
})
}
validateOnSubmit() {
this.form.addEventListener("submit", e => {
e.preventDefault(); //prevents the default action on the submit button
button.lastElementChild.style.display = "flex";
setTimeout(() => { button.lastElementChild.style.display = "none" }, 2000);
})
}
validateInput(field) {
if (field.value.trim() === "") {
this.setStatus(field, `${field.getAttribute('name')} cannot be blank!`, "error");
}
else {
if (field.type === "text") {
return this.validateName(field);
}
if (field.type === "email") {
return this.validateEmail(field);
}
if (field.id === "password") {
return this.validatePassword(field);
}
if (field.id === "confirm-password") {
return this.validatePwdConfirmation(field);
}
}
}
validateName(field) {
if (field.value.length > 1) {
const matchText = /^[a-zA-Z\s][^0-9]+$/;
if (matchText.test(field.value)) {
this.setStatus(field, null, "success");
field.value = field.value.trim();
}
else {
this.setStatus(field, "Use valid characters.", "error");
}
}
}
validateEmail(field) {
const matchEmail = /\S+@\S+.\S+/;
if (matchEmail.test(field.value)) {
this.setStatus(field, null, "success");
}
else {
this.setStatus(field, "Please input a valid email.", "error");
}
}
validatePassword(field) {
if (field.value.length < 8) {
this.setStatus(field, "Password should be 8 characters long.", "error");
}
else {
this.setStatus(field, null, "success");
}
}
validatePwdConfirmation(field) {
const password = this.form.querySelector('#password');
if (field.value !== password.value) {
this.setStatus(field, "Password does not match!", "error");
}
else {
this.setStatus(field, null, "success");
}
}
setStatus(field, message, status) {
const errorMessage = field.parentElement.querySelector('.error-message');
const icon = field.parentElement.querySelector('.icon');
if (status === "success") {
icon.classList.remove('red');
icon.classList.add('blue')
errorMessage.innerHTML = "";
}
if (status === "error") {
icon.classList.remove('blue');
icon.classList.add('red');
errorMessage.innerHTML = message;
}
}
}
class EnableButton {
constructor(form, fields) {
this.form = form;
this.fields = fields;
}
enableButton() {
this.form.addEventListener("input", e => {
const firstName = document.querySelector(`#${fields[0]}`);
const lastName = document.querySelector(`#${fields[1]}`);
const email = document.querySelector(`#${fields[2]}`);
const password = document.querySelector(`#${fields[3]}`);
const pwdConfirmation = document.querySelector(`#${fields[4]}`);
const checkbox = document.querySelector(`#${fields[5]}`);
if (this.validName(firstName) && this.validName(lastName) && this.validEmail(email) && this.validPassword(password) && this.validPwdConfirmation(pwdConfirmation) && this.checkedCheckbox(checkbox)) {
button.disabled = false;
}
else {
button.disabled = true;
}
})
}
validName(field) {
const matchText = /^[a-zA-Z\s][^0-9]+$/; //regex to check name format
if (matchText.test(field.value)) {
return true;
}
}
validEmail(field) {
const matchEmail = /\S+@\S+.\S+/; //regex to check email format
if (matchEmail.test(field.value)) {
return true;
}
}
validPassword(field) {
if (field.value.length >= 8) {
return true;
}
}
validPwdConfirmation(field) {
const password = this.form.querySelector('#password');
if (field.value == password.value) {
return true;
}
}
checkedCheckbox(field) {
if (field.checked) {
return true;
}
}
}
const form = document.querySelector('.form');
const fields = ["first-name", "last-name", "email", "password", "confirm-password", "checked"];
const button = document.querySelector('button');
const validator = new FormValidator(form, fields);
const btn = new EnableButton(form, fields);
validator.initialize();
btn.enableButton();