You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.3 KiB
60 lines
1.3 KiB
package tools
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"time"
|
|
)
|
|
|
|
// VerifyIdCard 身份证验证
|
|
func VerifyIdCard(idCard string) bool {
|
|
citys := []string{"11", "12", "13", "14", "15", "21", "22",
|
|
"23", "31", "32", "33", "34", "35", "36",
|
|
"37", "41", "42", "43", "44", "45", "46",
|
|
"50", "51", "52", "53", "54", "61", "62",
|
|
"63", "64", "65", "71", "81", "82", "91"}
|
|
|
|
// 长度格式验证
|
|
if !regexp.MustCompile("^([\\d]{14,18}[xX\\d]|[\\d]{15})$").MatchString(idCard) {
|
|
return false
|
|
}
|
|
// 省份验证
|
|
if !inArray(idCard[0:2], citys) {
|
|
return false
|
|
}
|
|
// 出生年月验证
|
|
if len(idCard) == 18 {
|
|
birth := fmt.Sprintf("%s-%s-%s", idCard[6:10], idCard[10:12], idCard[12:14])
|
|
birthday, err := time.Parse("2006-01-02", birth)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if birthday.Unix() > time.Now().Unix() {
|
|
return false
|
|
}
|
|
} else {
|
|
birth := fmt.Sprintf("19%s-%s-%s", idCard[6:8], idCard[8:10], idCard[10:12])
|
|
_, err := time.Parse("2006-01-02", birth)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// VerifyEmail 邮箱格式验证
|
|
func VerifyEmail(email string) bool {
|
|
pattern := `\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*` //匹配电子邮箱
|
|
reg := regexp.MustCompile(pattern)
|
|
return reg.MatchString(email)
|
|
}
|
|
|
|
func inArray(val string, arr []string) bool {
|
|
for _, s := range arr {
|
|
if s == val {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|