source: frontend/src/utils/userValidators.ts@ 700e2f9

main
Last change on this file since 700e2f9 was 700e2f9, checked in by 186079 <matej.milevski@…>, 5 days ago

Init

  • Property mode set to 100644
File size: 1.5 KB
Line 
1export interface PasswordValidationResult {
2 isValid: boolean;
3 errors: string[];
4}
5
6export interface UsernameValidationResult {
7 isValid: boolean;
8 errors: string[];
9}
10
11export const validateUsername = (
12 username: string,
13): UsernameValidationResult => {
14 const errors: string[] = [];
15
16 if (username.length < 3) {
17 errors.push("Username must be at least 3 characters long");
18 }
19
20 if (username.length > 50) {
21 errors.push("Username must not exceed 50 characters");
22 }
23
24 if (!/^[a-zA-Z0-9_.-]+$/.test(username)) {
25 errors.push(
26 "Username can only contain letters, numbers, dots, hyphens, and underscores",
27 );
28 }
29
30 return {
31 isValid: errors.length === 0,
32 errors,
33 };
34};
35
36export const validatePassword = (
37 password: string,
38): PasswordValidationResult => {
39 const errors: string[] = [];
40
41 if (password.length < 8) {
42 errors.push("Password must be at least 8 characters long");
43 }
44
45 if (password.length > 128) {
46 errors.push("Password must not exceed 128 characters");
47 }
48
49 if (!/[A-Z]/.test(password)) {
50 errors.push("Password must contain at least one uppercase letter");
51 }
52
53 if (!/[a-z]/.test(password)) {
54 errors.push("Password must contain at least one lowercase letter");
55 }
56
57 if (!/[0-9]/.test(password)) {
58 errors.push("Password must contain at least one number");
59 }
60
61 if (!/[!@#$%^&*()_+\-=\[\]{}|;:,.<>?]/.test(password)) {
62 errors.push(
63 "Password must contain at least one special character (!@#$%^&*()_+-=[]{}|;:,.<>?)",
64 );
65 }
66
67 return {
68 isValid: errors.length === 0,
69 errors,
70 };
71};
Note: See TracBrowser for help on using the repository browser.