github.com/profzone/eden-framework@v1.0.10/pkg/validate/validatetpl/zip.go (about)

     1  package validatetpl
     2  
     3  import (
     4  	"regexp"
     5  )
     6  
     7  const (
     8  	InvalidZipZhType  = "邮编类型错误"
     9  	InvalidZipZhValue = "无效的邮编"
    10  )
    11  
    12  var (
    13  	ZipZhValidRegx = regexp.MustCompile(`^[1-9]\d{5}$`)
    14  )
    15  
    16  func ValidateZipZh(v interface{}) (bool, string) {
    17  	s, ok := v.(string)
    18  	if !ok {
    19  		return false, InvalidZipZhType
    20  	}
    21  
    22  	if len(s) <= 0 {
    23  		return false, InvalidZipZhValue
    24  	}
    25  
    26  	if !ZipZhValidRegx.MatchString(s) {
    27  		return false, InvalidZipZhValue
    28  	}
    29  	return true, ""
    30  }
    31  
    32  func ValidateZipZhOrEmpty(v interface{}) (bool, string) {
    33  	s, ok := v.(string)
    34  	if !ok {
    35  		return false, InvalidZipZhType
    36  	}
    37  
    38  	if s != "" && !ZipZhValidRegx.MatchString(s) {
    39  		return false, InvalidZipZhValue
    40  	}
    41  	return true, ""
    42  }