github.com/profzone/eden-framework@v1.0.10/pkg/validate/validatetpl/ipv4.go (about) 1 package validatetpl 2 3 import ( 4 "regexp" 5 ) 6 7 const ( 8 InvalidIPv4Type = "IPv4类型错误" 9 InvalidIPv4Value = "无效的IPv4" 10 ) 11 12 var ( 13 reIpAddress = regexp.MustCompile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`) 14 ) 15 16 func ValidateIPv4(v interface{}) (bool, string) { 17 s, ok := v.(string) 18 if !ok { 19 return false, InvalidIPv4Type 20 } 21 22 if !reIpAddress.MatchString(s) { 23 return false, InvalidIPv4Value 24 } 25 return true, "" 26 } 27 28 func ValidateIPv4OrEmpty(v interface{}) (bool, string) { 29 s, ok := v.(string) 30 if !ok { 31 return false, InvalidIPv4Type 32 } 33 34 if s != "" && !reIpAddress.MatchString(s) { 35 return false, InvalidIPv4Value 36 } 37 return true, "" 38 }