github.com/profzone/eden-framework@v1.0.10/pkg/validate/validatetpl/password.go (about) 1 package validatetpl 2 3 import ( 4 "regexp" 5 ) 6 7 const ( 8 InvalidPasswordType = "密码类型错误" 9 InvalidPasswordValue = "无效的密码" 10 ) 11 12 var ( 13 passwordValidRegx = regexp.MustCompile(`^[0-9A-Za-z]{8,16}$`) 14 ) 15 16 func ValidatePassword(v interface{}) (bool, string) { 17 s, ok := v.(string) 18 if !ok { 19 return false, InvalidPasswordType 20 } 21 22 if len(s) <= 0 { 23 return false, InvalidPasswordValue 24 } 25 26 if !passwordValidRegx.MatchString(s) { 27 return false, InvalidPasswordValue 28 } 29 return true, "" 30 } 31 32 func ValidatePasswordOrEmpty(v interface{}) (bool, string) { 33 s, ok := v.(string) 34 if !ok { 35 return false, InvalidPasswordType 36 } 37 38 if s != "" && !passwordValidRegx.MatchString(s) { 39 return false, InvalidPasswordValue 40 } 41 return true, "" 42 }