79 lines
2.4 KiB
Go
79 lines
2.4 KiB
Go
package validation
|
||
|
||
import (
|
||
"errors"
|
||
"regexp"
|
||
)
|
||
|
||
// 使用预编译的全局正则表达式,避免重复创建和编译.
|
||
var (
|
||
lengthRegex = regexp.MustCompile(`^.{3,20}$`) // 长度在 3 到 20 个字符之间
|
||
validRegex = regexp.MustCompile(`^[A-Za-z0-9_]+$`) // 仅包含字母、数字和下划线
|
||
letterRegex = regexp.MustCompile(`[A-Za-z]`) // 至少包含一个字母
|
||
numberRegex = regexp.MustCompile(`\d`) // 至少包含一个数字
|
||
emailRegex = regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`) // 邮箱格式
|
||
phoneRegex = regexp.MustCompile(`^1[3-9]\d{9}$`) // 中国手机号
|
||
)
|
||
|
||
// IsValidUsername 校验用户名是否合法.
|
||
func IsValidUsername(username string) bool {
|
||
// 校验长度
|
||
if !lengthRegex.MatchString(username) {
|
||
return false
|
||
}
|
||
// 校验字符合法性
|
||
if !validRegex.MatchString(username) {
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
|
||
// IsValidPassword 判断密码是否符合复杂度要求.
|
||
func IsValidPassword(password string) error {
|
||
switch {
|
||
// 检查新密码是否为空
|
||
case password == "":
|
||
return errors.New("password cannot be empty")
|
||
// 检查新密码的长度要求
|
||
case len(password) < 6:
|
||
return errors.New("password must be at least 6 characters long")
|
||
// 使用正则表达式检查是否至少包含一个字母
|
||
case !letterRegex.MatchString(password):
|
||
return errors.New("password must contain at least one letter")
|
||
// 使用正则表达式检查是否至少包含一个数字
|
||
case !numberRegex.MatchString(password):
|
||
return errors.New("password must contain at least one number")
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// IsValidEmail 判断电子邮件是否合法.
|
||
func IsValidEmail(email string) error {
|
||
// 检查电子邮件地址格式
|
||
if email == "" {
|
||
return errors.New("email cannot be empty")
|
||
}
|
||
|
||
// 使用正则表达式校验电子邮件格式
|
||
if !emailRegex.MatchString(email) {
|
||
return errors.New("invalid email format")
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// IsValidPhone 判断手机号码是否合法.
|
||
func IsValidPhone(phone string) error {
|
||
// 检查手机号码格式
|
||
if phone == "" {
|
||
return errors.New("phone cannot be empty")
|
||
}
|
||
|
||
// 使用正则表达式校验手机号码格式(假设是中国手机号,11位数字)
|
||
if !phoneRegex.MatchString(phone) {
|
||
return errors.New("invalid phone format")
|
||
}
|
||
|
||
return nil
|
||
}
|