-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_client_side.js
228 lines (207 loc) · 7.14 KB
/
validate_client_side.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
const nameErr = getId('name_err');
// const usernameErr = getId('username_err');
const emailErr = getId('email_err');
const passwordErr = getId('password_err');
const confirmPasswordErr = getId('confirm_password_err');
const previousPasswordErr = getId('previous_password_err');
const newPasswordErr = getId('new_password_err');
const retypeNewPasswordErr = getId('retype_new_password_err');
const contactErr = getId('contact_err');
function validateName() {
const name = getId('name').value;
if (name.length === 0) {
innerHtml(nameErr, "Name is required");
nameErr.classList.add('text-danger');
return false;
}
if (!name.match(/[a-zA-Z\s]{3,35}/)) {
innerHtml(nameErr, "Invalid name");
nameErr.classList.add('text-danger');
return false;
}
innerHtml(nameErr, "Valid");
nameErr.classList.remove('text-danger');
nameErr.classList.add('text-success');
return true;
}
// function validateUsername() {
// const username = getId('username').value;
// if(username.length===0){
// innerHtml(usernameErr,"Username is required");
// return false;
// }
// if(!username.match(/^[\w]{5,15}$/)){
// innerHtml(usernameErr,"Invalid username");
// return false;
// }
// innerHtml(usernameErr,"Valid");
// return true;
// }
function validateEmail() {
const email = getId('email').value;
if (email.length === 0) {
innerHtml(emailErr, "Email is required");
emailErr.classList.add('text-danger');
return false;
}
if (!email.match(/[\w]{5,15}([-.]\w)*@[\w]+\.[\w]{2,3}/)) {
innerHtml(emailErr, "Invalid email");
emailErr.classList.add('text-danger');
return false;
}
innerHtml(emailErr, "Valid");
emailErr.classList.remove('text-danger');
emailErr.classList.add('text-success');
return true;
}
function validatePassword() {
const password = getId('password').value;
if (password.length === 0) {
innerHtml(passwordErr, "Password is required");
passwordErr.classList.add('text-danger');
return false;
}
if (!password.match(/(?!.*\s).{4,10}/)) {
innerHtml(passwordErr, "Password must be 4 to 10 characters long");
passwordErr.classList.add('text-danger');
return false;
}
innerHtml(passwordErr, "Valid");
passwordErr.classList.remove('text-danger');
passwordErr.classList.add('text-success');
return true;
}
function validateConfirmPassword() {
const password = getId('password').value;
const confirm_password = getId('confirm_password').value;
if (confirm_password.length === 0) {
innerHtml(confirmPasswordErr, "Confirm Password is required");
confirmPasswordErr.classList.add('text-danger');
return false;
}
if (!confirm_password.match(/(?!.*\s).{4,10}/)) {
innerHtml(confirmPasswordErr, "Invalid confirm password");
confirmPasswordErr.classList.add('text-danger');
return false;
}
if (password !== confirm_password) {
innerHtml(confirmPasswordErr, "Password does not match!");
return false;
}
innerHtml(confirmPasswordErr, "Valid");
confirmPasswordErr.classList.remove('text-danger');
confirmPasswordErr.classList.add('text-success');
return true;
}
function validatePreviousPassword() {
const previousPassword = getId('previous_password').value;
if (previousPassword.length === 0) {
innerHtml(previousPasswordErr, "Current Password is required");
previousPasswordErr.classList.add('text-danger');
return false;
}
if (!previousPassword.match(/(?!.*\s).{4,10}/)) {
innerHtml(previousPasswordErr, "Password must be 4 to 10 characters long");
previousPasswordErr.classList.add('text-danger');
return false;
}
innerHtml(previousPasswordErr, "Valid");
previousPasswordErr.classList.remove('text-danger');
previousPasswordErr.classList.add('text-success');
return true;
}
function validateNewPassword() {
const newPassword = getId('new_password').value;
if (newPassword.length === 0) {
innerHtml(newPasswordErr, "New Password is required");
newPasswordErr.classList.add('text-danger');
return false;
}
if (!newPassword.match(/(?!.*\s).{4,10}/)) {
innerHtml(newPasswordErr, "Password must be 4 to 10 characters long");
newPasswordErr.classList.add('text-danger');
return false;
}
innerHtml(newPasswordErr, "Valid");
newPasswordErr.classList.remove('text-danger');
newPasswordErr.classList.add('text-success');
return true;
}
function validateRetypeNewPassword() {
const previousPassword = getId('previous_password').value;
const retypeNewPassword = getId('retype_new_password').value;
if (retypeNewPassword.length === 0) {
innerHtml(retypeNewPasswordErr, "Retype New Password is required");
retypeNewPasswordErr.classList.add('text-danger');
return false;
}
if (!retypeNewPassword.match(/(?!.*\s).{4,10}/)) {
innerHtml(retypeNewPasswordErr, "Invalid retype new password");
retypeNewPasswordErr.classList.add('text-danger');
return false;
}
if (previousPassword !== retypeNewPassword) {
innerHtml(retypeNewPasswordErr, "Password does not match!");
return false;
}
innerHtml(retypeNewPasswordErr, "Valid");
retypeNewPasswordErr.classList.remove('text-danger');
retypeNewPasswordErr.classList.add('text-success');
return true;
}
function validateContact() {
const contact = getId('contact').value;
if (contact.length === 0) {
innerHtml(contactErr, "Contact is required");
contactErr.classList.add('text-danger');
return false;
}
if (!contact.match(/01[3-9]{1}[0-9]{8}/)) {
innerHtml(contactErr, "Invalid contact");
contactErr.classList.add('text-danger');
return false;
}
innerHtml(contactErr, "Valid");
contactErr.classList.remove('text-danger');
contactErr.classList.add('text-success');
return true;
}
function validateRole() {
const role_as = getId('role_as').value;
if (role_as.length === 0) {
innerHtml(role_asErr, "Role is required");
role_asErr.classList.add('text-danger');
return false;
}
innerHtml(role_asErr, "Valid");
role_asErr.classList.remove('text-danger');
role_asErr.classList.add('text-success');
return true;
}
function getId(name) {
const id = document.getElementById(name);
return id;
}
function innerHtml(name, text) {
name.innerHTML = text;
}
function formData() {
if (!validateName() || !validateEmail() || !validatePassword() || !validateConfirmPassword() || !validateContact())
return false;
else return true;
}
function loginFormData() {
if (!validateEmail() || !validatePassword())
return false;
else return true;
}
function adminFormData() {
if (!validateName() || !validateEmail())
return false;
else return true;
}
function adminFormPassData() {
if (!validatePreviousPassword() || !validateNewPassword() || !validateRetypeNewPassword())
return false;
else return true;
}