-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathform-validation.js
More file actions
80 lines (62 loc) · 1.99 KB
/
form-validation.js
File metadata and controls
80 lines (62 loc) · 1.99 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// creating instant validation
const checkSignIn = (email, password) => {
if (!email || !password) {
return "Please fill in all fields and try again.";
}
return "Success";
};
const checkRegister = (fname, lname, email, password, confirmPass) => {
const errorList = [];
if (!fname || !lname || !email || !password || !confirmPass) {
errorList.push("Please fill in all fields.")
}
const firstNameCheck = validName(fname, "first name");
if (firstNameCheck !== "Success") {
errorList.push(firstNameCheck)
}
const lastNameCheck = validName(lname, "last name");
if (lastNameCheck !== "Success") {
errorList.push(lastNameCheck)
}
const emailCheck = validEmail(email);
if (emailCheck !== "Success") {
errorList.push(emailCheck)
}
const passCheck = validPassword(password);
if (passCheck !== "Success") {
errorList.push(passCheck)
}
if (password !== confirmPass) {
errorList.push("Please make sure your two passwords match.")
}
return errorList.length ? errorList : "Success"
};
const validEmail = email => {
// regex taken from https://www.w3resource.com/javascript/form/email-validation.php
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
return "Success";
}
return "Please enter a valid email address.";
};
// EXTREMELY BASIC - SUBJECT TO IMPROVEMENTS IN FUTURE
const validPassword = password => {
if (password.length < 6) {
return "Please enter a valid password - Password must contain at least six characters.";
}
return "Success";
};
// for checking names upon register - max length is 50, first letter must be Capital.
const validName = (name, type) => {
// remove all whitespaces and check if empty string
if (!name.replace(/\s/g, '').length) {
return `Please enter a valid ${type} with characters.`
}
if (!(/^[a-zA-Z\s]*$/.test(name))) {
return `Please ensure you only entered alphabets in the ${type} field`
}
return "Success"
};
module.exports = {
checkSignIn,
checkRegister
};