package main
import "regexp"
func main() {
isIdentityCard(`123456789012345`)
isIdentityCard(`12345678901234567X`)
}
// 识别身份证
func isIdentityCard(identityCard string) {
switch len(identityCard) {
case 15:
// 15位身份证号码:15位全是数字
result, _ := regexp.MatchString(`^(\d{15})$`, identityCard)
msgIdentityCard(result, `位数:15位`)
case 18:
// 18位身份证:前17位为数字,第18位为校验位,可能是数字或X
result, _ := regexp.MatchString(`^(\d{17})([0-9]|X|x)$`, identityCard)
msgIdentityCard(result, `位数:18位`)
default:
msgIdentityCard(false, `身份证位数应该为:15位 与 18位`)
}
}
// 自定义身份证提示信息
func msgIdentityCard(result bool, msg string) {
if result {
println(`正确的身份证`, msg)
} else {
println(`错误的身份证`, msg)
}
}
Go语言:检验是不是一个身份证(包含18位身份证)
最新推荐文章于 2022-04-30 21:28:02 发布