-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathvalidateRegisterForm.js
More file actions
66 lines (54 loc) · 1.49 KB
/
validateRegisterForm.js
File metadata and controls
66 lines (54 loc) · 1.49 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
'use strict';
/**
* @param {string} email
*
* @param {string} password
*
* @returns {object}
*/
function validateRegisterForm(email, password) {
// eslint-disable-next-line max-len
const validPassword = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,16}$/;
// eslint-disable-next-line max-len
const validEmail = new RegExp(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\./i);
let isValidEmail = email
? email.match(validEmail)
: false;
const isValidPassword = password
? password.match(validPassword)
: false;
const emailTopDomain = function() {
const doHaveDomain = isValidEmail[2];
if (doHaveDomain) {
const index = email.lastIndexOf(isValidEmail[2]) + isValidEmail[2].length;
const isStartWithDot = email.startsWith('.', index + 1);
const isEndWithDot = email.endsWith('.');
const isCorrect = !isStartWithDot && !isEndWithDot;
if (!isCorrect) {
isValidEmail = false;
}
}
};
if (isValidEmail) {
emailTopDomain();
}
if (!isValidEmail && isValidPassword) {
return {
code: 422, message: 'Email is invalid.',
};
}
if (isValidEmail && !isValidPassword) {
return {
code: 422, message: 'Password is invalid.',
};
}
if (!isValidEmail && !isValidPassword) {
return {
code: 500, message: 'Password and email are invalid.',
};
}
return {
code: 200, message: 'Email and password are valid.',
};
}
module.exports = validateRegisterForm;