130 lines
2.5 KiB
Go
130 lines
2.5 KiB
Go
package utils
|
||
|
||
import (
|
||
"regexp"
|
||
"strings"
|
||
"unicode"
|
||
)
|
||
|
||
// IsEmail 验证是否是邮箱格式
|
||
func IsEmail(email string) bool {
|
||
if strings.TrimSpace(email) == "" {
|
||
return false
|
||
}
|
||
|
||
// 邮箱正则表达式
|
||
pattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
|
||
matched, err := regexp.MatchString(pattern, email)
|
||
if err != nil {
|
||
return false
|
||
}
|
||
return matched
|
||
}
|
||
|
||
// IsMobile 验证是否是手机号(中国手机号格式)
|
||
func IsMobile(mobile string) bool {
|
||
if strings.TrimSpace(mobile) == "" {
|
||
return false
|
||
}
|
||
|
||
// 中国手机号正则表达式:1开头,第二位3-9,后面9位数字
|
||
pattern := `^1[3-9]\d{9}$`
|
||
matched, err := regexp.MatchString(pattern, mobile)
|
||
if err != nil {
|
||
return false
|
||
}
|
||
return matched
|
||
}
|
||
|
||
// IsNumber 验证是否是纯数字
|
||
func IsNumber(str string) bool {
|
||
if strings.TrimSpace(str) == "" {
|
||
return false
|
||
}
|
||
|
||
for _, char := range str {
|
||
if !unicode.IsDigit(char) {
|
||
return false
|
||
}
|
||
}
|
||
return true
|
||
}
|
||
|
||
// IsEnglish 验证是否是英文字符(不限大小写)
|
||
func IsEnglish(str string) bool {
|
||
if strings.TrimSpace(str) == "" {
|
||
return false
|
||
}
|
||
|
||
for _, char := range str {
|
||
if !unicode.IsLetter(char) {
|
||
return false
|
||
}
|
||
if !isEnglishLetter(char) {
|
||
return false
|
||
}
|
||
}
|
||
return true
|
||
}
|
||
|
||
// IsEnglishWithSpace 验证是否是英文字符(允许空格)
|
||
func IsEnglishWithSpace(str string) bool {
|
||
if strings.TrimSpace(str) == "" {
|
||
return false
|
||
}
|
||
|
||
for _, char := range str {
|
||
if unicode.IsSpace(char) {
|
||
continue
|
||
}
|
||
if !unicode.IsLetter(char) {
|
||
return false
|
||
}
|
||
if !isEnglishLetter(char) {
|
||
return false
|
||
}
|
||
}
|
||
return true
|
||
}
|
||
|
||
// IsAlphanumeric 验证是否是字母和数字组合
|
||
func IsAlphanumeric(str string) bool {
|
||
if strings.TrimSpace(str) == "" {
|
||
return false
|
||
}
|
||
|
||
for _, char := range str {
|
||
if !unicode.IsLetter(char) && !unicode.IsDigit(char) {
|
||
return false
|
||
}
|
||
}
|
||
return true
|
||
}
|
||
|
||
// IsStrongPassword 验证强密码(至少8位,包含大小写字母和数字)
|
||
func IsStrongPassword(password string) bool {
|
||
if len(password) < 8 {
|
||
return false
|
||
}
|
||
|
||
var hasUpper, hasLower, hasDigit bool
|
||
|
||
for _, char := range password {
|
||
switch {
|
||
case unicode.IsUpper(char):
|
||
hasUpper = true
|
||
case unicode.IsLower(char):
|
||
hasLower = true
|
||
case unicode.IsDigit(char):
|
||
hasDigit = true
|
||
}
|
||
}
|
||
|
||
return hasUpper && hasLower && hasDigit
|
||
}
|
||
|
||
// 辅助函数:判断是否是英文字母
|
||
func isEnglishLetter(char rune) bool {
|
||
return (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z')
|
||
}
|