-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
validate.ts
36 lines (32 loc) · 924 Bytes
/
validate.ts
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
const emailRegex = /^([\w%+-]+\.)*[\w%+-]+@([A-Z0-9-]+\.)+[A-Z0-9-]{2,}$/i;
const passwordLength = 8;
const nameLength = 50;
const usernameLength = 50;
export function validateEmail(email: FormDataEntryValue) {
if (emailRegex.test(email?.toString())) {
return true;
} else {
throw new Error('Invalid email address');
}
}
export function validatePassword(password: FormDataEntryValue) {
if (password.toString().length >= passwordLength) {
return true;
} else {
throw new Error(`Password must be at least ${passwordLength} characters long`);
}
}
export function validateName(name: FormDataEntryValue) {
if (name.toString().length <= nameLength) {
return true;
} else {
throw new Error(`Name is too long`);
}
}
export function validateUsername(username: FormDataEntryValue) {
if (username.toString().length <= usernameLength) {
return true;
} else {
throw new Error(`Username is too long`);
}
}