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

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