validators.js
1.07 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
module.exports.validateRegisterInput = (
username,
email,
password,
confirmPassword
) => {
const errors = {};
if (username.trim() === "") {
errors.username = "Username must not be empty";
}
if (email.trim() === "") {
errors.email = "Email must not be empty";
} else {
const regEx = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
if (!email.match(regEx)) {
errors.email = "Email must be a valid email address";
}
}
if (password.trim() === "") {
errors.password = "Password must not be empty";
} else if (password !== confirmPassword) {
errors.confirmPassword = "Passwords must match";
}
return {
errors,
valid: Object.keys(errors).length < 1
};
};
module.exports.validateLoginInput = (username, password) => {
const errors = {};
if (username.trim() === "") {
errors.username = "Username must not be empty";
}
if (password.trim() === "") {
errors.password = "Password must not be empty";
}
return {
errors,
valid: Object.keys(errors).length < 1
};
};