66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package util
|
||
|
||
import (
|
||
"regexp"
|
||
"unicode"
|
||
)
|
||
|
||
// ValidateEmail 验证邮箱格式
|
||
func ValidateEmail(email string) bool {
|
||
emailRegex := regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
|
||
return emailRegex.MatchString(email)
|
||
}
|
||
|
||
// ValidatePhone 验证手机号格式
|
||
func ValidatePhone(phone string) bool {
|
||
// 支持国际格式:+8613812345678
|
||
// 或国内格式:13812345678
|
||
phoneRegex := regexp.MustCompile(`^\+?[1-9]\d{1,14}$`)
|
||
return phoneRegex.MatchString(phone)
|
||
}
|
||
|
||
// ValidateUsername 验证用户名格式
|
||
func ValidateUsername(username string) bool {
|
||
// 用户名:3-20个字符,只能包含字母、数字、下划线
|
||
if len(username) < 3 || len(username) > 20 {
|
||
return false
|
||
}
|
||
|
||
usernameRegex := regexp.MustCompile(`^[a-zA-Z0-9_]+$`)
|
||
return usernameRegex.MatchString(username)
|
||
}
|
||
|
||
// ValidateNickname 验证昵称格式
|
||
func ValidateNickname(nickname string) bool {
|
||
// 昵称:1-50个字符,不能包含敏感字符
|
||
if len(nickname) == 0 || len(nickname) > 50 {
|
||
return false
|
||
}
|
||
|
||
// 检查是否包含表情符号(可选)
|
||
for _, r := range nickname {
|
||
if unicode.Is(unicode.Sk, r) {
|
||
return false
|
||
}
|
||
}
|
||
|
||
return true
|
||
}
|
||
|
||
// ValidateRealName 验证真实姓名
|
||
func ValidateRealName(name string) bool {
|
||
// 真实姓名:2-50个字符,只能是中文或英文字母
|
||
if len(name) < 2 || len(name) > 50 {
|
||
return false
|
||
}
|
||
|
||
// 检查是否只包含中文和英文字母
|
||
for _, r := range name {
|
||
if !unicode.Is(unicode.Han, r) && !unicode.IsLetter(r) && r != '·' && r != ' ' {
|
||
return false
|
||
}
|
||
}
|
||
|
||
return true
|
||
}
|