Files
common-server-public/archive/bbs-go/server/internal/pkg/validate/validate.go
12600k-rog-d4 8f4d399cfd 20251209
2025-12-09 20:33:57 +08:00

77 lines
2.0 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 validate
import (
"errors"
"regexp"
"strings"
"github.com/mlogclub/simple/common/strs"
)
// IsUsername 验证用户名合法性用户名必须由5-12位(数字、字母、_、-)组成,且必须以字母开头。
func IsUsername(username string) error {
if strs.IsBlank(username) {
return errors.New("请输入用户名")
}
matched, err := regexp.MatchString("^[0-9a-zA-Z_-]{5,12}$", username)
if err != nil || !matched {
return errors.New("用户名必须由5-12位(数字、字母、_、-)组成,且必须以字母开头")
}
matched, err = regexp.MatchString("^[a-zA-Z]", username)
if err != nil || !matched {
return errors.New("用户名必须由5-12位(数字、字母、_、-)组成,且必须以字母开头")
}
return nil
}
// IsEmail 验证是否是合法的邮箱
func IsEmail(email string) (err error) {
if strs.IsBlank(email) {
err = errors.New("邮箱格式不符合规范")
return
}
pattern := `^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$`
matched, _ := regexp.MatchString(pattern, email)
if !matched {
err = errors.New("邮箱格式不符合规范")
}
return
}
// IsValidPassword 是否是合法的密码
func IsValidPassword(password, rePassword string) error {
if err := IsPassword(password); err != nil {
return err
}
if password != rePassword {
return errors.New("两次输入密码不匹配")
}
return nil
}
func IsPassword(password string) error {
if strs.IsBlank(password) {
return errors.New("请输入密码")
}
if strs.RuneLen(password) < 6 {
return errors.New("密码过于简单")
}
if strs.RuneLen(password) > 1024 {
return errors.New("密码长度不能超过128")
}
return nil
}
// IsURL 是否是合法的URL
func IsURL(url string) error {
if strs.IsBlank(url) {
return errors.New("URL格式错误")
}
indexOfHttp := strings.Index(url, "http://")
indexOfHttps := strings.Index(url, "https://")
if indexOfHttp == 0 || indexOfHttps == 0 {
return nil
}
return errors.New("URL格式错误")
}