Repository navigation
Expand file tree
/
Copy pathvalidation.js
More file actions
130 lines (113 loc) Β· 3.21 KB
/
Copy pathvalidation.js
File metadata and controls
130 lines (113 loc) Β· 3.21 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Email validation
*/
export const validateEmail = (email) => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
};
/**
* Password validation with strength levels
* Returns { isValid: boolean, strength: 'weak' | 'medium' | 'strong', message: string }
*/
export const validatePassword = (password) => {
const minLength = 8;
const hasUpperCase = /[A-Z]/.test(password);
const hasLowerCase = /[a-z]/.test(password);
const hasNumbers = /\d/.test(password);
const hasSpecialChar = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password);
if (password.length < minLength) {
return {
isValid: false,
strength: "weak",
message: `Password must be at least ${minLength} characters long`,
};
}
let strength = "weak";
let score = 0;
if (password.length >= minLength) score++;
if (hasUpperCase) score++;
if (hasLowerCase) score++;
if (hasNumbers) score++;
if (hasSpecialChar) score++;
if (score <= 2) strength = "weak";
else if (score <= 3) strength = "medium";
else strength = "strong";
return {
isValid: score >= 3,
strength,
message:
score < 3
? "Password should contain uppercase, lowercase, numbers, and special characters"
: `Password strength: ${strength}`,
};
};
/**
* Name validation
*/
export const validateName = (name) => {
const trimmedName = name.trim();
if (trimmedName.length < 2) {
return { isValid: false, message: "Name must be at least 2 characters long" };
}
if (trimmedName.length > 50) {
return { isValid: false, message: "Name must not exceed 50 characters" };
}
if (!/^[a-zA-Z\s'-]+$/.test(trimmedName)) {
return { isValid: false, message: "Name contains invalid characters" };
}
return { isValid: true, message: "Name is valid" };
};
/**
* Form validation groups
*/
export const validateLoginForm = (email, password) => {
const errors = {};
if (!email) {
errors.email = "Email is required";
} else if (!validateEmail(email)) {
errors.email = "Please enter a valid email address";
}
if (!password) {
errors.password = "Password is required";
} else if (password.length < 6) {
errors.password = "Password must be at least 6 characters";
}
return {
isValid: Object.keys(errors).length === 0,
errors,
};
};
export const validateSignupForm = (parentName, email, password) => {
const errors = {};
// Validate parent name
const nameValidation = validateName(parentName);
if (!nameValidation.isValid) {
errors.parentName = nameValidation.message;
}
// Validate email
if (!email) {
errors.email = "Email is required";
} else if (!validateEmail(email)) {
errors.email = "Please enter a valid email address";
}
// Validate password
const passwordValidation = validatePassword(password);
if (!passwordValidation.isValid) {
errors.password = passwordValidation.message;
}
return {
isValid: Object.keys(errors).length === 0,
errors,
};
};
/**
* Sanitize input - Basic XSS prevention
*/
export const sanitizeInput = (input) => {
return String(input)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
};