Completed
Push — master ( c38c9f...6d2c0d )
by Josh
06:54
created

public_html/js/FormValidationAPI.js   B

Complexity

Total Complexity 52
Complexity/F 4

Size

Lines of Code 355
Function Count 13

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
eloc 195
nc 32
dl 0
loc 355
rs 7.44
c 0
b 0
f 0
wmc 52
mnd 2
bc 72
fnc 13
bpm 5.5384
cpm 4
noi 10

12 Functions

Rating   Name   Duplication   Size   Complexity  
A FormValidationAPI.validateNewJobPosterForm 0 17 2
A FormValidationAPI.fieldNotEmpty 0 3 2
A FormValidationAPI.setValidationErrorProperties 0 10 2
A FormValidationAPI.validateTwitterUsername 0 4 1
A FormValidationAPI.passwordMatch 0 3 1
A FormValidationAPI.validateLoginForm 0 28 5
A FormValidationAPI.focusIfFirstInvalidField 0 5 2
F FormValidationAPI.validateJobPoster 0 159 19
A FormValidationAPI.validateEmail 0 4 1
A FormValidationAPI.validateUpdateProfileStep1 0 35 4
B FormValidationAPI.validateRegisterForm 0 36 7
A FormValidationAPI.validateUpdateProfileBasicInfo 0 16 3

How to fix   Complexity   

Complexity

Complex classes like public_html/js/FormValidationAPI.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
7
8
9
var FormValidationAPI = {};
10
11
/**
12
 *
13
 * @param {string} emailToValidate
14
 * @returns {Boolean}
15
 */
16
FormValidationAPI.validateEmail = function(emailToValidate){
17
    var 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,}))$/;
18
    return regex.test(emailToValidate);
19
};
20
21
/**
22
 *
23
 * @param {string} password1
24
 * @param {string} password2
25
 * @returns {Boolean}
26
 */
27
FormValidationAPI.passwordMatch = function(password1, password2){
28
    return password1 === password2;
29
};
30
31
FormValidationAPI.validateLoginForm = function(email, password) {
32
    var credentials = {};
33
    var valid = true;
34
35
    FormValidationAPI.setValidationErrorProperties(false, "login_email_error", "login_email_error_msg");
36
37
    //console.log(FormValidationAPI.validateEmail(email));
38
    if(FormValidationAPI.fieldNotEmpty(email)){
39
        if(!FormValidationAPI.validateEmail(email)) {
40
            valid = false;
41
            FormValidationAPI.setValidationErrorProperties(true, "login_email_error", "login_email_error_msg", "Error: Invalid email address.");
42
        }
43
    }else{
44
        FormValidationAPI.setValidationErrorProperties(true, "login_email_error", "login_email_error_msg", "Error: Please enter your mail address.");
45
    }
46
47
    FormValidationAPI.setValidationErrorProperties(false, "login_password_error", "login_password_error_msg");
48
    if(!FormValidationAPI.fieldNotEmpty(password)){
49
        valid = false;
50
        FormValidationAPI.setValidationErrorProperties(true, "login_password_error", "login_password_error_msg", "Error: Please enter your password.");
51
    }
52
53
    if(valid) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if valid is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
54
        credentials.email = encodeURIComponent(email);
55
        credentials.password = encodeURIComponent(password);
56
        return credentials;
57
    }
58
};
59
60
FormValidationAPI.validateRegisterForm = function(email, email_confirm, password, confirm_password) {
61
    var valid = true;
62
63
    /*FormValidationAPI.setValidationErrorProperties(false, "register_name_error", "register_name_error_msg");
64
    if(name < 2 || !name) {
65
        valid = false;
66
        FormValidationAPI.setValidationErrorProperties(true, "register_name_error", "register_name_error_msg", "Error: Invalid name.");
67
    }*/
68
69
    FormValidationAPI.setValidationErrorProperties(false, "register_email_confirm_error", "register_email_confirm_error_msg");
70
    if(email !== email_confirm) {
71
        valid = false;
72
        FormValidationAPI.setValidationErrorProperties(true, "register_email_confirm_error", "register_email_confirm_error_msg", "Error: Emails do not match.");
73
    }
74
75
    FormValidationAPI.setValidationErrorProperties(false, "register_email_error", "register_email_error_msg");
76
    if((FormValidationAPI.validateEmail(email)).length === 0 || !FormValidationAPI.validateEmail(email)) {
77
        valid = false;
78
        FormValidationAPI.setValidationErrorProperties(true, "register_email_error", "register_email_error_msg", "Error: Invalid email.");
79
    }
80
81
    FormValidationAPI.setValidationErrorProperties(false, "register_password_confirm_error", "register_password_confirm_error_msg");
82
    if(!FormValidationAPI.passwordMatch(password, confirm_password)) {
83
        valid = false;
84
        FormValidationAPI.setValidationErrorProperties(true, "register_password_confirm_error", "register_password_confirm_error_msg", "Error: Passwords do not match.");
85
    }
86
87
    FormValidationAPI.setValidationErrorProperties(false, "register_password_error", "register_password_error_msg");
88
    if(password.length < 6 || !password) {
89
        valid = false;
90
        //FormValidationAPI.setValidationErrorProperties(true, "register_password_confirm_error", "register_password_confirm_error_msg", "Error: Invalid password.");
91
        FormValidationAPI.setValidationErrorProperties(true, "register_password_error", "register_password_error_msg", "Error: Password must be at least 6 characters.");
92
    }
93
94
    return valid;
95
};
96
97
FormValidationAPI.validateUpdateProfileBasicInfo = function(twitter, linkedin) {
0 ignored issues
show
Unused Code introduced by
The parameter linkedin is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
98
    var valid = true;
99
        
100
    if(FormValidationAPI.fieldNotEmpty(twitter) && !FormValidationAPI.validateTwitterUsername(twitter)) {
101
        FormValidationAPI.setValidationErrorProperties(true, "profileEditTwitterError", "profileEditTwitterErrorMsg", "Error: Invalid Twitter Username");
102
        FormValidationAPI.focusIfFirstInvalidField(valid, "profileEditTwitter")
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
103
        valid = false;
104
    } else {
105
        FormValidationAPI.setValidationErrorProperties(false, "profileEditTwitterError", "profileEditTwitterErrorMsg", "Error: Invalid Twitter Username");
106
    }
107
108
109
    //TODO: validate linkedin url
110
111
    return valid;
112
};
113
114
FormValidationAPI.validateTwitterUsername = function(twitterUsername) {
115
    var regex = /^@[0-9a-zA-Z_]+$/;
116
    return regex.test(twitterUsername);
117
};
118
119
FormValidationAPI.focusIfFirstInvalidField = function(isFirstInvalidField, fieldId) {
120
    if (isFirstInvalidField) {
121
        document.getElementById(fieldId).focus();
122
    }
123
};
124
125
FormValidationAPI.validateUpdateProfileStep1 = function(name, link, accomplishment, accomplishment_fr) {
126
    var valid = true;
127
    /*if(!FormValidationAPI.fieldNotEmpty(name)){
128
        FormValidationAPI.setValidationErrorProperties(true, "createEditProfile_name_error", "createEditProfile_name_error_msg", "Error: No Name");
129
        valid = false;
130
    }
131
    else{
132
        FormValidationAPI.setValidationErrorProperties(false, "createEditProfile_name_error", "createEditProfile_name_error_msg", "Error: No Name");
133
    }
134
*/
135
    if(!FormValidationAPI.fieldNotEmpty(link)){
136
        FormValidationAPI.setValidationErrorProperties(true, "createEditProfile_link_error", "createEditProfile_link_error_msg", "Error: Need valid link");
137
        valid = false;
138
    }else{
139
        FormValidationAPI.setValidationErrorProperties(false, "createEditProfile_link_error", "createEditProfile_link_error_msg", "Error: Need valid link");
140
    }
141
142
    if(!FormValidationAPI.fieldNotEmpty(accomplishment)){
143
        FormValidationAPI.setValidationErrorProperties(true, "createEditProfile_accomplishment_error", "createEditProfile_accomplishment_error_msg", "Error: Need accomplishment");
144
        valid = false;
145
    }
146
    else{
147
        FormValidationAPI.setValidationErrorProperties(false, "createEditProfile_accomplishment_error", "createEditProfile_accomplishment_error_msg", "Error: Need accomplishment");
148
    }
149
150
    if(!FormValidationAPI.fieldNotEmpty(accomplishment_fr)){
151
        FormValidationAPI.setValidationErrorProperties(true, "createEditProfile_accomplishment_fr_error", "createEditProfile_accomplishment_fr_error_msg", "Error: Need accomplishment_fr");
152
        valid = false;
153
    }
154
    else{
155
        FormValidationAPI.setValidationErrorProperties(false, "createEditProfile_accomplishment_fr_error", "createEditProfile_accomplishment_fr_error_msg", "Error: Need accomplishment_fr");
156
    }
157
158
    return valid;
159
};
160
161
FormValidationAPI.validateUpdateProfileStep1 = function(b_exp, w_exp, sup, b_exp_fr, w_exp_fr, sup_fr) {
0 ignored issues
show
Unused Code introduced by
The parameter sup_fr is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter w_exp_fr is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
162
    var valid = true;
0 ignored issues
show
Unused Code introduced by
The variable valid seems to be never used. Consider removing it.
Loading history...
163
    if(!FormValidationAPI.fieldNotEmpty(b_exp) && !FormValidationAPI.fieldNotEmpty(b_exp_fr)){
164
        FormValidationAPI.setValidationErrorProperties(true, "createEditProfile_best_work_exp_error", "createEditProfile_best_work_exp_error_msg", "Error: No work exp");
165
        valid = false;
166
    }
167
    else {
168
        FormValidationAPI.setValidationErrorProperties(false, "createEditProfile_best_work_exp_error", "createEditProfile_best_work_exp_error_msg", "Error: No work exp");
169
    }
170
};
171
172
FormValidationAPI.validateNewJobPosterForm = function(name, closeDate, id, department) {
0 ignored issues
show
Unused Code introduced by
The parameter department is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter id is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter closeDate is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
173
    var valid = true;
174
175
    FormValidationAPI.setValidationErrorProperties(false, "newJobPoster_title_error", "newJobPoster_title_error_msg");
176
    if(name === ''){
177
        valid = false;
178
        FormValidationAPI.setValidationErrorProperties(true, "newJobPoster_title_error", "newJobPoster_title_error_msg", "Error: Invalid job title");
179
    }
180
181
    //Check closeDate
182
183
    //if(id is not equal to the session manager's value of id)  valid = false
184
185
    //Validate that the department matches session information
186
187
    return valid;
188
};
189
190
FormValidationAPI.validateJobPoster = function(
191
    title_en, title_fr, department_id, branch_en, branch_fr, division_en, division_fr, province_id, city_en, city_fr, open_date_time,
192
    close_date_time, start_date, term_qty, remuneration_range_low, remuneration_range_high, classification, clearance_id, language_id) {
193
    var valid = true;
194
195
    if(!FormValidationAPI.fieldNotEmpty(title_en)){
196
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_jobTitle_error", "createJobPoster_jobTitle_error_msg", "Error: No Job Title");
197
        valid = false;
198
    }
199
    else{
200
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_jobTitle_error", "createJobPoster_jobTitle_error_msg", "Error: No Job Title");
201
    }
202
203
    if(!FormValidationAPI.fieldNotEmpty(title_fr)){
204
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_jobTitle_fr_error", "createJobPoster_jobTitle_fr_error_msg", "Error: No Job Title");
205
        valid = false;
206
    }
207
    else{
208
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_jobTitle_fr_error", "createJobPoster_jobTitle_fr_error_msg", "Error: No Job Title");
209
    }
210
211
    if(!FormValidationAPI.fieldNotEmpty(department_id)){
212
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_department_error", "createJobPoster_department_error_msg", "Error: No Department Selected");
213
        valid = false;
214
    }
215
    else{
216
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_department_error", "createJobPoster_department_error_msg", "Error: No Department Selected");
217
    }
218
219
    if(!FormValidationAPI.fieldNotEmpty(branch_en)){
220
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_branch_error", "createJobPoster_branch_error_msg", "Error: No Branch");
221
        valid = false;
222
    }
223
    else{
224
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_branch_error", "createJobPoster_branch_error_msg", "Error: Branch");
225
    }
226
227
    if(!FormValidationAPI.fieldNotEmpty(branch_fr)){
228
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_branch_fr_error", "createJobPoster_branch_fr_error_msg", "Error: No Branch");
229
        valid = false;
230
    }
231
    else{
232
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_branch_fr_error", "createJobPoster_branch_fr_error_msg", "Error: No Division");
233
    }
234
235
    if(!FormValidationAPI.fieldNotEmpty(division_en)){
236
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_division_error", "createJobPoster_division_error_msg", "Error: No Division");
237
        valid = false;
238
    }
239
    else{
240
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_division_error", "createJobPoster_division_error_msg", "Error: No Division");
241
    }
242
243
    if(!FormValidationAPI.fieldNotEmpty(division_fr)){
244
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_division_fr_error", "createJobPoster_division_fr_error_msg", "Error: No Division");
245
        valid = false;
246
    }
247
    else{
248
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_division_fr_error", "createJobPoster_division_fr_error_msg", "Error: No Division");
249
    }
250
251
    if(!FormValidationAPI.fieldNotEmpty(province_id)){
252
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_province_error", "createJobPoster_province_error_msg", "Error: No Province Selected");
253
        valid = false;
254
    }
255
    else{
256
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_province_error", "createJobPoster_province_error_msg", "Error: No Province Selected");
257
    }
258
259
    if(!FormValidationAPI.fieldNotEmpty(city_en)){
260
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_city_error", "createJobPoster_city_error_msg", "Error: No City");
261
        valid = false;
262
    }
263
    else{
264
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_city_error", "createJobPoster_city_error_msg", "Error: No City");
265
    }
266
267
    /*if(!FormValidationAPI.fieldNotEmpty(city_fr)){
268
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_city_fr_error", "createJobPoster_city_fr_error_msg", "Error: No City");
269
        valid = false;
270
    }
271
    else{
272
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_city_fr_error", "createJobPoster_city_fr_error_msg", "Error: No City");
273
    }*/
274
275
    if(!FormValidationAPI.fieldNotEmpty(open_date_time)){
276
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_openDate_error", "createJobPoster_openDate_error_msg", "Error: No Open Date/Time");
277
        valid = false;
278
    }
279
    else{
280
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_openDate_error", "createJobPoster_openDate_error_msg", "Error: No Open Date/Time");
281
    }
282
283
    if(!FormValidationAPI.fieldNotEmpty(close_date_time)){
284
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_closeDate_error", "createJobPoster_closeDate_error_msg", "Error: No Close Date/Time");
285
        valid = false;
286
    }
287
    else{
288
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_closeDate_error", "createJobPoster_closeDate_error_msg", "Error: No Close Date/Time");
289
    }
290
291
    if(!FormValidationAPI.fieldNotEmpty(start_date)){
292
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_startDate_error", "createJobPoster_startDate_error_msg", "Error: No Start Date");
293
        valid = false;
294
    }
295
    else{
296
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_startDate_error", "createJobPoster_startDate_error_msg", "Error: No Start Date");
297
    }
298
299
    if(!FormValidationAPI.fieldNotEmpty(term_qty)){
300
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_termQuantity_error", "createJobPoster_termQuantity_error_msg", "Error: No Term Duration");
301
        valid = false;
302
    }
303
    else{
304
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_termQuantity_error", "createJobPoster_termQuantity_error_msg", "Error: No Term Duration");
305
    }
306
307
    if(!FormValidationAPI.fieldNotEmpty(remuneration_range_low)){
308
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_remunerationLowRange_error", "createJobPoster_remunerationLowRange_error_msg", "Error: No minimum salary");
309
        valid = false;
310
    }
311
    else{
312
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_remunerationLowRange_error", "createJobPoster_remunerationLowRange_error_msg", "Error: No minimum salary");
313
    }
314
315
    if(!FormValidationAPI.fieldNotEmpty(remuneration_range_high)){
316
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_remunerationHighRange_error", "createJobPoster_remunerationHighRange_error_msg", "Error: No maximum salary");
317
        valid = false;
318
    }
319
    else{
320
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_remunerationHighRange_error", "createJobPoster_remunerationHighRange_error_msg", "Error: No maximum salary");
321
    }
322
323
    if(!FormValidationAPI.fieldNotEmpty(classification)){
324
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_classification_error", "createJobPoster_classification_error_msg", "Error: No Classification");
325
        valid = false;
326
    }
327
    else{
328
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_classification_error", "createJobPoster_classification_error_msg", "Error: No Classification");
329
    }
330
331
    if(!FormValidationAPI.fieldNotEmpty(clearance_id)){
332
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_clearance_error", "createJobPoster_clearance_error_msg", "Error: No Security Clearance Selected");
333
        valid = false;
334
    }
335
    else{
336
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_clearance_error", "createJobPoster_clearance_error_msg", "Error: No Security Clearance Selected");
337
    }
338
339
    if(!FormValidationAPI.fieldNotEmpty(language_id)){
340
        FormValidationAPI.setValidationErrorProperties(true, "createJobPoster_language_error", "createJobPoster_language_error_msg", "Error: No Language Selected");
341
        valid = false;
342
    }
343
    else{
344
        FormValidationAPI.setValidationErrorProperties(false, "createJobPoster_language_error", "createJobPoster_language_error_msg", "Error: No Language Selected");
345
    }
346
347
    return valid;
348
}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
349
350
FormValidationAPI.setValidationErrorProperties = function(isVisible, errorId, msgId, msg) {
351
    var errorElement = document.getElementById(errorId);
352
    var msgElement = document.getElementById(msgId);
353
    if(isVisible) {
354
        errorElement.classList.remove("hidden");
355
    } else {
356
        errorElement.classList.add("hidden");
357
    }
358
    msgElement.textContent = msg;
359
};
360
361
FormValidationAPI.fieldNotEmpty = function(fieldValue){
362
    return fieldValue.length > 0 ? true : false;
363
};
364