Files
cz-web/src/utils/validate.ts
wangzzpeng 7bc84bbe5e first
2026-09-24 10:10:43 +08:00

37 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 校验规则(无 Vue 依赖)
/** 手机号 */
export function isPhone(value: string): boolean {
return /^1[3-9]\d{9}$/.test(value)
}
/** 邮箱 */
export function isEmail(value: string): boolean {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)
}
/** URL */
export function isUrl(value: string): boolean {
return /^https?:\/\/[^\s]+$/.test(value)
}
export type PasswordLevel = 'weak' | 'medium' | 'strong'
/**
* 密码强度等级(6-20 位)
* weak:仅字母或仅数字;medium:字母+数字;strong:字母+数字+特殊符号
*/
export function getPasswordLevel(password: string): PasswordLevel {
const hasLetter = /[a-zA-Z]/.test(password)
const hasDigit = /\d/.test(password)
const hasSymbol = /[^\w\s]/.test(password)
if (hasLetter && hasDigit && hasSymbol) return 'strong'
if (hasLetter && hasDigit) return 'medium'
return 'weak'
}
/** 登录密码合法性(6-20 位) */
export function isValidPassword(password: string): boolean {
return password.length >= 6 && password.length <= 20
}