76 lines
1.5 KiB
Go
76 lines
1.5 KiB
Go
package validator
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
// MinPasswordLength 最小密码长度
|
|
MinPasswordLength = 6
|
|
// MaxPasswordLength 最大密码长度
|
|
MaxPasswordLength = 50
|
|
// MinNicknameLength 最小昵称长度
|
|
MinNicknameLength = 1
|
|
// MaxNicknameLength 最大昵称长度
|
|
MaxNicknameLength = 50
|
|
)
|
|
|
|
var (
|
|
// 中国手机号正则表达式
|
|
mobileRegex = regexp.MustCompile(`^1[3-9]\d{9}$`)
|
|
)
|
|
|
|
// ValidateMobile 验证手机号格式
|
|
func ValidateMobile(mobile string) bool {
|
|
if mobile == "" {
|
|
return false
|
|
}
|
|
return mobileRegex.MatchString(mobile)
|
|
}
|
|
|
|
// ValidatePassword 验证密码强度
|
|
func ValidatePassword(password string) (bool, string) {
|
|
if password == "" {
|
|
return false, "password cannot be empty"
|
|
}
|
|
|
|
if len(password) < MinPasswordLength {
|
|
return false, "password too short"
|
|
}
|
|
|
|
if len(password) > MaxPasswordLength {
|
|
return false, "password too long"
|
|
}
|
|
|
|
return true, ""
|
|
}
|
|
|
|
// ValidateNickname 验证昵称
|
|
func ValidateNickname(nickname string) (bool, string) {
|
|
if nickname == "" {
|
|
return false, "nickname cannot be empty"
|
|
}
|
|
|
|
nickname = strings.TrimSpace(nickname)
|
|
if len(nickname) < MinNicknameLength {
|
|
return false, "nickname too short"
|
|
}
|
|
|
|
if len(nickname) > MaxNicknameLength {
|
|
return false, "nickname too long"
|
|
}
|
|
|
|
return true, ""
|
|
}
|
|
|
|
// ValidateStarID 验证star_id
|
|
func ValidateStarID(starID int64) bool {
|
|
return starID > 0
|
|
}
|
|
|
|
// ValidateUserID 验证user_id
|
|
func ValidateUserID(userID int64) bool {
|
|
return userID > 0
|
|
}
|