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

     1  package validatetpl
     2  
     3  import (
     4  	"regexp"
     5  )
     6  
     7  const (
     8  	InvalidEmailType      = "邮箱类型错误"
     9  	InvalidEmailValue     = "无效的邮箱"
    10  	EmailMaxLength    int = 128
    11  )
    12  
    13  var (
    14  	emailValidRegx = regexp.MustCompile(`^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$`)
    15  )
    16  
    17  func ValidateEmail(v interface{}) (bool, string) {
    18  	s, ok := v.(string)
    19  	if !ok {
    20  		return false, InvalidEmailType
    21  	}
    22  
    23  	length := len(s)
    24  	if length <= 0 || length > EmailMaxLength {
    25  		return false, InvalidEmailValue
    26  	}
    27  
    28  	if !emailValidRegx.MatchString(s) {
    29  		return false, InvalidEmailValue
    30  	}
    31  	return true, ""
    32  }
    33  
    34  func ValidateEmailOrEmpty(v interface{}) (bool, string) {
    35  	s, ok := v.(string)
    36  	if !ok {
    37  		return false, InvalidEmailType
    38  	}
    39  
    40  	length := len(s)
    41  	if length > EmailMaxLength {
    42  		return false, InvalidEmailValue
    43  	}
    44  
    45  	if s != "" && !emailValidRegx.MatchString(s) {
    46  		return false, InvalidEmailValue
    47  	}
    48  	return true, ""
    49  }