-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelperfunctions.js
67 lines (66 loc) · 2.36 KB
/
helperfunctions.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
export function validateAuthInputs(purpose, valStatus, email, password, confirmPassword, username, level){
// 1. validate email
const emailRegex = new RegExp(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/);
if(!emailRegex.test(email) || email.length > 320){
//if email does not follow the regex pattern, or longer than 320 characters (maximum length of an email)
valStatus = {
...valStatus,
error: true,
emailError: "Email entered is not valid."
}
}
if(purpose === "register"){
// 2. validate password
if(password != confirmPassword){
valStatus = {
...valStatus,
error: true,
passwordError: "Passwords entered should be the same."
}
} else if(password.length < 8){
valStatus = {
...valStatus,
error: true,
passwordError: "Password needs to be at least 8 characters."
}
}
// 3. validate username
const usernameRegex = new RegExp(/^[a-zA-Z0-9]+$/);
if(username.length > 50){
valStatus = {
...valStatus,
error: true,
usernameError: "Username cannot be longer than 50 characters."
}
}else if(!usernameRegex.test(username)){
valStatus = {
...valStatus,
error: true,
usernameError: "Username can only contain letters and numbers."
}
}
// 4. validate level input
if(level === ""){
// Tests if the level string only contains numbers
valStatus = {
...valStatus,
error: true,
levelError: "Please choose a level for your account"
}
} else if (!(/^\d+$/.test(level))){
valStatus = {
...valStatus,
error: true,
levelError: "Invalid level. Level should be numeric"
}
} else if (parseInt(level) < 1 || parseInt(level) > 12){
valStatus = {
...valStatus,
error: true,
levelError: "Invalid level. Level cannot be less than 1 or more than 12"
}
}
}
// 5. Return the valStatus
return valStatus;
}