身份证格式验证
valid:=validation.Validation{}
reg := regexp.MustCompile(`^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`)
valid.Match(this.GetString("identification_number"),reg,"identification_number").Message("identification_number format is fail")
var datas []string
if valid.HasErrors(){
for _,err := range valid.Errors {
datas = append(datas,err.Message)
}
this.Data["json"]= this.HTTP_ERROR_WITH_DATA(datas)
this.ServeJSON()
this.StopRun()
}
身份证获取年龄信息
func GetAgeWithIdentificationNumber(identification_number string)int{
reg := regexp.MustCompile(`^[1-9]\d{5}(18|19|20)(\d{2})((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`)
//reg := regexp.MustCompile(`^[1-9]\d{5}(18|19|20)`)
params :=reg.FindStringSubmatch(identification_number)
birYear,_:= strconv.Atoi(params[1]+params[2])
birMonth,_ := strconv.Atoi(params[3])
age :=time.Now().Year()-birYear
if int(time.Now().Month()) <birMonth{
age --
}
return age
}
有疑问加站长微信联系(非本文作者)