Home / PostsPost
Golang验证手机号、邮箱
嘟噜聪2020/11/11 15:44:23 [Golang] 4330人已阅
简介 Golang使用正则验证手机号、邮箱
Golang验证手机号、邮箱
正则验证是否是手机:
//mobile verify
func VerifyMobileFormat(mobileNum string) bool {
regular := "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$"
reg := regexp.MustCompile(regular)
return reg.MatchString(mobileNum)
}
下则验证是否是邮箱:
//email verify
func VerifyEmailFormat(email string) bool {
//pattern := `\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*` //匹配电子邮箱
pattern := `^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z].){1,4}[a-z]{2,4}$`
reg := regexp.MustCompile(pattern)
return reg.MatchString(email)
}
很赞哦! (3)