diff --git a/validate.go b/validate.go index 88a0dd5..d3535c2 100644 --- a/validate.go +++ b/validate.go @@ -3,6 +3,7 @@ package helper import ( "errors" "regexp" + "strconv" "strings" ) @@ -67,3 +68,21 @@ func IsEmail(email string) bool { reg := regexp.MustCompile(pattern) return reg.MatchString(email) } + +func ValidateIds(s string) bool { + if s == "" { + return false + } + parts := strings.Split(s, ",") + for _, part := range parts { + part = strings.TrimSpace(part) + if part == "" { + return false // 不允许空段(如 "123,,456") + } + id, err := strconv.Atoi(part) + if err != nil || id < 0 { + return false // 非数字或负数均视为无效 + } + } + return true +}