-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.tsx
77 lines (67 loc) · 2.63 KB
/
utils.tsx
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
export { wait, isValidNumber, validEmailFormat, validatePasswordStrengthMid ,validYoutubeUrl};
function wait(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}
function isValidNumber(num: any) {
return typeof num === 'number' && Number.isFinite(num)
}
function validEmailFormat(email: string): boolean {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
function validYoutubeUrl(url: string): boolean {
const regex = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/(watch\?v=)?([a-zA-Z0-9_-]{11})(&t=([0-9]+m[0-9]+s))?$/;
return regex.test(url);
}
/**
* 校验密码强度,使用最大强度校验
* 密码长度至少8位,包含至少一个大写字母,一个小写字母,一个数字,一个特殊字符
* @param password
* @returns
*/
function validatePasswordStrengthMax(password: string): { valid: boolean, message: string } {
const minLength = 8;
const hasUpperCase = /[A-Z]/.test(password);
const hasLowerCase = /[a-z]/.test(password);
const hasNumber = /\d/.test(password);
const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password);
if (password.length < minLength) {
return { valid: false, message: "Password must be at least 8 characters long." };
}
if (!hasUpperCase) {
return { valid: false, message: "Password must contain at least one uppercase letter." };
}
if (!hasLowerCase) {
return { valid: false, message: "Password must contain at least one lowercase letter." };
}
if (!hasNumber) {
return { valid: false, message: "Password must contain at least one number." };
}
if (!hasSpecialChar) {
return { valid: false, message: "Password must contain at least one special character." };
}
return { valid: true, message: "Password is strong." };
}
/**
* 校验密码强度,使用中等强度校验
* 密码长度至少8位,包含至少一个字母,一个数字
* @param password
* @returns
*/
function validatePasswordStrengthMid(password: string): { valid: boolean, message: string } {
const minLength = 8;
const hasUpperCase = /[A-Z]/.test(password);
const hasLowerCase = /[a-z]/.test(password);
const hasNumber = /\d/.test(password);
const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password);
if (password.length < minLength) {
return { valid: false, message: "Password must be at least 8 characters long." };
}
if (!hasUpperCase && !hasLowerCase) {
return { valid: false, message: "Password must contain at least one letter." };
}
if (!hasNumber) {
return { valid: false, message: "Password must contain at least one number." };
}
return { valid: true, message: "Password is strong." };
}