30 lines
744 B
Go
30 lines
744 B
Go
package required
|
|
|
|
import (
|
|
"regexp"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
type Option struct {
|
|
ValidatorName string
|
|
ValidatorFunc func(fl validator.FieldLevel) bool
|
|
}
|
|
|
|
// WithAccount 創建一個新的 Option 結構,包含自定義的驗證函數,用於驗證 email 和台灣的手機號碼格式
|
|
func WithAccount(tagName string) Option {
|
|
return Option{
|
|
ValidatorName: tagName,
|
|
ValidatorFunc: func(fl validator.FieldLevel) bool {
|
|
value := fl.Field().String()
|
|
emailRegex := `^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,}$`
|
|
phoneRegex := `^(\+886|0)?9\d{8}$`
|
|
|
|
emailMatch, _ := regexp.MatchString(emailRegex, value)
|
|
phoneMatch, _ := regexp.MatchString(phoneRegex, value)
|
|
|
|
return emailMatch || phoneMatch
|
|
},
|
|
}
|
|
}
|