mingyang-admin-iot-app/rpc/internal/util/validator_utils.go

66 lines
1.5 KiB
Go
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.

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
}