asaskevich / govalidator

[Go] Package of validators and sanitizers for strings, numerics, slices and structs
MIT License
6.02k stars 556 forks source link

IsUTFLetterNumeric #372

Closed alifpay closed 3 years ago

alifpay commented 4 years ago

I think your example is not working. alphanumspace is better Title: "My Example Post" has spaces, so ValidateStruct returns err

type Post struct {
    Title    string `valid:"alphanum,required"`
    Message  string `valid:"duck,ascii"`
    Message2 string `valid:"animal(dog)"`
    AuthorIP string `valid:"ipv4"`
    Date     string `valid:"-"`
}
post := &Post{
    Title:   "My Example Post",
    Message: "duck",
    Message2: "dog",
    AuthorIP: "123.234.54.3",
}
result, err := govalidator.ValidateStruct(post)
if err != nil {
    println("error: " + err.Error())
}
println(result)
func IsUTFLetterNumericSpace(str string) bool {
        str = strings.TrimSpace(str)
    if IsNull(str) {
        return true
    }

    for _, c := range str {
        if !unicode.IsLetter(c) && !unicode.IsNumber(c) && !unicode.IsSpace(c) {
            return false
        }
    }
    return true

}
ygj6 commented 3 years ago

@alifpay What kind of problem are you trying to describe? Add IsAlphanumericSpace and set IsAlphanumericSpace("My Example Post") to true?