28 lines
674 B
Go
28 lines
674 B
Go
package util
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
mainlandMobileRe = regexp.MustCompile(`^1[3-9]\d{9}$`)
|
|
trimCharsRe = regexp.MustCompile(`[\s\-()]`)
|
|
)
|
|
|
|
// NormalizeMobile 归一化手机号:去空白/连字符/括号,去 +86 与 86 国家码前缀。
|
|
func NormalizeMobile(s string) string {
|
|
s = trimCharsRe.ReplaceAllString(s, "")
|
|
if strings.HasPrefix(s, "+86") {
|
|
s = s[3:]
|
|
} else if strings.HasPrefix(s, "86") && len(s) > 11 {
|
|
s = s[2:]
|
|
}
|
|
return s
|
|
}
|
|
|
|
// IsValidMainlandMobile 校验大陆手机号:归一化后须为 1[3-9] 开头的 11 位数字。
|
|
func IsValidMainlandMobile(s string) bool {
|
|
return mainlandMobileRe.MatchString(NormalizeMobile(s))
|
|
}
|